I'm extremely new to Java, and I had a bit of an attempt at making something that you could call lotto. I'v still got a lot more to go on it, but things are becoming tedious, and I am sure there is a simpler way to do what I am doing. So, as the title asks, how can this code be changed to have less repetitive code/optimized?
import java.util.Random;
import java.util.Scanner;
public class Lotto {
//Pick 6 numbers from 1 to 100
//If all of your numbers gets called out, you win
public static void wait (int n) {
long t0,t1;
t0=System.currentTimeMillis();
do{
t1=System.currentTimeMillis();
}
while (t1-t0<1000);
}
public static void main(String[] args) {
Random rand = new Random();
Scanner numScan1 = new Scanner(System.in);
Scanner numScan2 = new Scanner(System.in);
Scanner numScan3 = new Scanner(System.in);
Scanner numScan4 = new Scanner(System.in);
Scanner numScan5 = new Scanner(System.in);
Scanner numScan6 = new Scanner(System.in);
boolean num1Cor = false;
boolean num2Cor = false;
boolean num3Cor = false;
boolean num4Cor = false;
boolean num5Cor = false;
boolean num6Cor = false;
System.out.print("Pick 6 numbers from 0 to 25. Pick your first number: ");
int num1 = numScan1.nextInt();
System.out.println();
System.out.print("Pick your second number: ");
int num2 = numScan2.nextInt();
System.out.println();
System.out.print("Pick your third number: ");
int num3 = numScan3.nextInt();
System.out.println();
System.out.print("Pick your fourth number: ");
int num4 = numScan4.nextInt();
System.out.println();
System.out.print("Pick your fifth number: ");
int num5 = numScan5.nextInt();
System.out.println();
System.out.print("Pick your final number: ");
int num6 = numScan6.nextInt();
System.out.println("Numbers will now start to be drawn, if all of your numbers are called, you win.");
while (true) {
int random = rand.nextInt(25);
System.out.println("Number " + random + ".");
if (random == num1) {
num1Cor = true;
System.out.println("Your number, " + num1 + ", has been called.");
}
else if (random == num2) {
num2Cor = true;
System.out.println("Your number, " + num2 + ", has been called.");
}
else if (random == num3) {
num3Cor = true;
System.out.println("Your number, " + num3 + ", has been called.");
}
else if (random == num4) {
num4Cor = true;
System.out.println("Your number, " + num4 + ", has been called.");
}
else if (random == num5) {
num5Cor = true;
System.out.println("Your number, " + num5 + ", has been called.");
}
else if (random == num6) {
num6Cor = true;
System.out.println("Your number, " + num6 + ", has been called.");
}
if (num1Cor == true && num2Cor == true && num3Cor == true && num4Cor == true && num5Cor == true && num6Cor == true) {
System.out.println("You win!!");
break;
}
wait(100);
}
}
}
Scanner
– Jigar Joshi Dec 26 '11 at 5:45