0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paiza 勉強記録【宝くじ】【Java】

Last updated at Posted at 2024-12-30

paizaでの勉強記録です。
今回は下記のレベルアップ問題集のCランク問題の「宝くじ」を実施しました。
各賞の判定に用いる数字を変数に設定して、最後にそれと宝くじの番号の比較を行っていますがもう少しスマートにしたい、、、特に前後賞あたりとか
あと各賞に当選しているかの判定は別でメソッドを定義して、mainメソッドではそれを呼び出して受け取った結果から何等かを出力するようにした方がアーキテクチャ的にはいいのだろうか、、、

import java.util.Scanner;

public class paiza {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int prizeNumber = sc.nextInt();
        int purchaseNumber = sc.nextInt();

//        前後賞の番号の設定
        String upperPrizeNumber;
        String underPrizeNumber;
        
        if (prizeNumber==100000) {
        	upperPrizeNumber = Integer.toString(prizeNumber+1);
        	underPrizeNumber = "0";
		} else if (prizeNumber==199999) {
			upperPrizeNumber = "0";
			underPrizeNumber = Integer.toString(prizeNumber-1);
		} else {
			upperPrizeNumber = Integer.toString(prizeNumber+1);
			underPrizeNumber = Integer.toString(prizeNumber-1);
		}
        
//        2等賞、3等賞の番号の設定
        Integer prizeNumberInteger = Integer.valueOf(prizeNumber);
        String secondPrizeNumber = prizeNumberInteger.toString().substring(2, 6);
        String thirdPrizeNumber = prizeNumberInteger.toString().substring(3, 6);;
        
//        各宝くじの当選チェック
        for (int i = 0; i < purchaseNumber; i++) {
        	String ticketNumber = sc.next();
        	if (ticketNumber.equals(prizeNumberInteger.toString())) {
        		System.out.println("first");
        	}else if (ticketNumber.equals(upperPrizeNumber) || ticketNumber.equals(underPrizeNumber)) {
        		System.out.println("adjacent");
			} else if (ticketNumber.substring(2, 6).equals(secondPrizeNumber)) {
				System.out.println("second");
			} else if (ticketNumber.substring(3, 6).equals(thirdPrizeNumber)) {
				System.out.println("third");
			} else {
				System.out.println("blank");
			}
		}
        
        sc.close();
    }
}

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?