patternjavaMinor
Reduce repetitive code in Lotto simulator
Viewed 0 times
reducerepetitivecodesimulatorlotto
Problem
I'm extremely new to Java, and I had a bit of an attempt at making something that you could call lotto. I've 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. 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();
```
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();
Solution
First, you need just one
Second, you could store the number choose by the user in an array.
Third, you could iterate the array to find the number there, instead of doing a lot of
Forth, the user always wins. This looks like a bug. The while(true) ensures that the numbers are choosen in a way that eventually all the choosen numbers will be drawn. More, nothing stops the user from choosing a number more than once.
Fifth, the
Scanner.Second, you could store the number choose by the user in an array.
Third, you could iterate the array to find the number there, instead of doing a lot of
if and else if.Forth, the user always wins. This looks like a bug. The while(true) ensures that the numbers are choosen in a way that eventually all the choosen numbers will be drawn. More, nothing stops the user from choosing a number more than once.
Fifth, the
wait(100) will fail. You should use Thread.sleep(100) instead. EDIT, oops, I thought you were using Object.wait. It currently works, but Thread.sleep is better.Context
StackExchange Code Review Q#7160, answer score: 8
Revisions (0)
No revisions yet.