[Java] Compare values / strings (AOJ14 --card game)

Compare values and strings

import java.util.Date;
public class Main {
 
	public static void main(String[] args) {
 
		String str1 = "a";
		String str2 = "b";
		String str3 = "A";
 		Date date1 = new Date(2017, 3, 28, 16, 20, 22);
		Date date2 = new Date(2017, 3, 29, 16, 20, 24);
 
		System.out.println(str1.compareTo(str2)); //-1
		System.out.println(str2.compareTo(str1)); //1
		System.out.println(str1.compareTo(str1)); //0
		System.out.println(str1.compareTo(str3)); //32
		System.out.println("===");
		System.out.println(str1.compareToIgnoreCase(str3)); //0
		System.out.println("===");
		System.out.println(date1.compareTo(date2)); //-1
		System.out.println(date2.compareTo(date1)); //1
		System.out.println(date1.compareTo(date1)); //0
	}
}

Card game (ITP1-9)

Taro and Hanako play a card game. Each of them has n cards and plays n turns. Take out one card each turn. The name of the animal in the alphabet is written on the card, and the one with the largest alphabetical order wins the turn. 3 points will be added to the winner, and 1 point will be added to each draw. Create a program that reads the information on the cards that Taro and Hanako have and outputs the scores after the game is over. Constraints

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int Taro = 0;
		int Hanako = 0;

		for (int i = 0; i < a; i++) {
			String b = scanner.next();
			String c = scanner.next();
			if (b.compareTo(c) > 0)
				Taro += 3;
			else if (b.compareTo(c) < 0)
				Hanako += 3;
			else {
				Taro += 1;
				Hanako += 1;
			}
		}
		System.out.println(String.format("%d %d",Taro, Hanako));
	}
}

Recommended Posts

[Java] Compare values / strings (AOJ14 --card game)
Manipulating Java strings
Reverse Enum constants from strings and values in Java
Rock-paper-scissors game java practice
Compare Lists in Java