[Java] Character acquisition / boolean type usage (AOJ⑤ Finding missing cards)

Get one character at the specified position from the character string

public class Main {
    public static void main(String[] args){

    String str = "Mofumofuneko";
    System.out.println(str.charAt(0)); //Also
    System.out.println(str.charAt(3)); //Fu
    System.out.println(str.charAt(4)); //Ne
    System.out.println(str.charAt(5)); //This
  }
}

Use of boolean type

//Numerical comparison
public class Main {
    public static void main(String[] args){
    System.out.println("1 = 1 ? : " + (1 == 1)); //1 = 1 ? : true 
    System.out.println("1 = 2 ? : " + (1 == 2)); //1 = 2 ? : false
    }
}

How to use in conditional branching of if statement

//Indicates the branch direction with an if statement
int a = 0;
int b = 1;
if(a == b) {
  System.out.println("Run if true");
}
else {
  System.out.println("Execute if false");
}
public class Main {
    public static void main(String[] args){
        //Declare a boolean variable
        boolean isEighteen = false; 
        int Age = 18; 
        isEighteen = (Age >= 18); 
        System.out.println(isEighteen); //true
        if (isEighteen) { // true 
            System.out.println("18 years and over"); //18 years and over
        } else {
            System.out.println("Under 18");
        }
    }
}

How to use while

//Determine whether to continue the loop with a while statement
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
    int i = 0;
    while(i < 10) {
        System.out.print(i + " ");
        i += 1;
    }
  }
}

Boolean

public class Main {
    public static void main(String[] args){
        Boolean bool = Boolean.valueOf(true);
        System.out.println(bool); //true
        bool = null;
        System.out.println(bool); //null
  }
}
public class Main {
    public static void main(String[] args){
        Boolean bool = Boolean.valueOf(true);
        System.out.println(bool.toString()); //true
    }
}

Finding the missing card

When Taro tries to play cards with Hanako, he has only n cards, which should be 52. Create a program that takes these n cards as input and outputs the missing cards. Taro's first playing card was 52 cards, excluding the Joker. The 52 cards are divided into 4 patterns: spades, hearts, clubs, and diamonds, and each pattern has 13 ranks.

import java.util.*;
class Main {

	static char[] symbol = new char[] {'S','H','C','D'};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		boolean[][] cards = new boolean[4][13];
		for(int i = 0; i < n; i++) {
			char k = sc.next().charAt(0);
			int r = sc.nextInt();
                         //Note that the array starts at 0!
			if(k == 'S') {
				cards[0][r-1] = true;
			}
			if(k == 'H') {
				cards[1][r-1] = true;
			}
			if(k == 'C') {
				cards[2][r-1] = true;
			}
			if(k == 'D') {
				cards[3][r-1] = true;
			}
		}
		for(int i = 0 ; i< 4; i++) {
			for(int j = 0; j < 13; j++) {
				if(!cards[i][j]) {
					System.out.println( symbol[i] + " " + (j+1));
				}
			}
		}
	}
}

Recommended Posts

[Java] Character acquisition / boolean type usage (AOJ⑤ Finding missing cards)
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
[Java] Character judgment / character string formatting (AOJ11 --character count)
[Java] Comparison of String type character strings