0
0

More than 3 years have passed since last update.

【Java】文字取得・boolean型用途(AOJ⑤不足しているカードの発見)

Posted at

文字列から指定位置の1文字を取得

  • charAtメソッド
  • 文字列.charAt(指定位置);
public class Main {
    public static void main(String[] args){

    String str = "もふもふねこ";
    System.out.println(str.charAt(0)); //も
    System.out.println(str.charAt(3)); //ふ
    System.out.println(str.charAt(4)); //ね
    System.out.println(str.charAt(5)); //こ
  }
}

boolean型の用途

  • boolean型は、true(はい)またはfalse(いいえ)の判定結果を入れるために利用できる
  • 数値や文字列の比較結果を表す
  • if文で分岐の方向を表す
  • for文やwhile文でループを続行するか決定する
//数値比較
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
    }
}

if文の条件分岐で使う方法

//if文で分岐の方向を表す
int a = 0;
int b = 1;
if(a == b) {
  System.out.println("trueの場合に実行");
}
else {
  System.out.println("falseの場合に実行");
}
public class Main {
    public static void main(String[] args){
        // boolean型変数を宣言
        boolean isEighteen = false; 
        int Age = 18; 
        isEighteen = (Age >= 18); 
        System.out.println(isEighteen); //true
        if (isEighteen) { // true 
            System.out.println("18歳以上"); //18歳以上
        } else {
            System.out.println("18歳未満");
        }
    }
}

Whileで使う方法

//while文でループを続行するか決定
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

  • Booleanとは、boolean型ラッパークラスのことで、boolean型を便利に使うためのメソッドを持ちます。
  • Booleanクラス 参照型 クラスまたはインスタンスを参照する
  • Booleanはクラスなのでnull(何も定義されていない参照)
  • toStringメソッド
    • boolean型の値を文字列で返し、trueが入っている時は“true”、falseは“false”が返る
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
    }
}

不足しているカードの発見

太郎が花子と一緒にトランプ遊びをしようとしたところ、52枚あるはずのカードが n 枚のカードしか手元にありません。これらの n 枚のカードを入力として、足りないカードを出力するプログラムを作成して下さい。
太郎が最初に持っていたトランプはジョーカーを除く52枚のカードです。
52枚のカードは、スペード、ハート、クラブ、ダイヤの4つの絵柄に分かれており、各絵柄には13のランクがあります。
* Input
最初の行に太郎が持っているカードの枚数 n (n ≤ 52)が与えられます。
続いて n 組のカードがそれぞれ1行に与えられます。各組は1つの空白で区切られた文字と整数です。文字はカードの絵柄を表し、スペードが'S'、ハートが'H'、クラブが'C'、ダイヤが'D'で表されています。整数はそのカードのランク(1 〜 13)を表しています。
* Output
足りないカードをそれぞれ1行に出力して下さい。各カードは入力と同様に1つの空白で区切られた文字と整数です。出力するカードの順番は以下のとおりとします:
* 絵柄がスペード、ハート、クラブ、ダイヤの順番で優先的に出力する。
* 絵柄が同じ場合は、ランクが小さい順に出力する。

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();
                         //配列は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));
                }
            }
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0