Help us understand the problem. What is going on with this article?

ArrayListの練習

More than 1 year has passed since last update.

6桁を選んで、当選番号と同じならあたり
今回は宝くじを買うところまで

Lotto.java
package javaStudy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Lotto {

    public static void main(String[] args) {
        ArrayList<Integer> yourChoice = new ArrayList<>(6);
        Scanner scan = new Scanner(System.in);
        ArrayList<Integer> number = new ArrayList<>(43);
        for(int i = 0;i < 43;i++) {
            number.add(i+1);
        }       
        System.out.println("購入方法を選んでください。 0 = 自分で選ぶ 1 = ランダム");
        System.out.print("あなたの回答: ");
        int a = scan.nextInt();
        if(a == 0) {
            do {
                System.out.print((yourChoice.size()+1)+"つ目の数字を入力してください :");
                int i = scan.nextInt();
                if(yourChoice.contains(i)) {
                    System.out.println("[エラー]その数字はすでに選んでいます。別の数字を入力して下さい。");
                    continue;
                }
                try {
                    yourChoice.add(number.get(i-1));
                }catch(IndexOutOfBoundsException e) {
                    System.out.println("[エラー]選べる数字は1〜43です。");
                }
            }while(yourChoice.size()!= 6);
        }else if(a == 1) {
            Collections.shuffle(number);
            for(int i = 0;i < 6;i++) {
                yourChoice.add(number.get(i));
            }
        }
        System.out.println("あなたは" + yourChoice + "を選びました。");
    }
}
sumy
Java勉強中です。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away