ArrayList practice

Select 6 digits and if it is the same as the winning number This time to the point of buying a lottery ticket

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("Please choose the purchase method. 0=Choose for yourself 1=random");
		System.out.print("Your answer: ");
		int a = scan.nextInt();
		if(a == 0) {
			do {
				System.out.print((yourChoice.size()+1)+"Please enter the second number:");
				int i = scan.nextInt();
				if(yourChoice.contains(i)) {
					System.out.println("[error]I have already selected that number. Please enter another number.");
					continue;
				}
				try {
					yourChoice.add(number.get(i-1));
				}catch(IndexOutOfBoundsException e) {
					System.out.println("[error]The numbers you can choose are 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("you are" + yourChoice + "I chose.");
	}
}


Recommended Posts

[Practice] ArrayList
[Practice] ArrayList
ArrayList practice
ArrayList
Array practice
Array practice 2
[Practice] Enumeration
ArrayList class
if else practice
String Buffer practice
java practice part 1
[Practice] Map method