patternjavaMinor
Three-number lottery simulator
Viewed 0 times
threesimulatornumberlottery
Problem
Please let me know how I can make it better, especially with avoiding repetition without the need to replace the user guesses.
```
import java.util.Scanner;
public class Lottery {
static int guess[] = new int[3],actual[]= new int[3];
private static void aquireGuess()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Time to play the lottery please choose three numbers ranging from zero to nine.");
System.out.print("Please enter first number:");
guess[0] = keyboard.nextInt();
System.out.print("Please enter second number:");
guess[1] = keyboard.nextInt();
System.out.print("Please enter third number:");
guess[2] = keyboard.nextInt();
keyboard.close();
}
private static void generateLottery()
{
actual[0] = (int)(Math.random()*10);
actual[1] = (int)(Math.random()*10);
actual[2]= (int)(Math.random()*10);
}
private static void compare()
{
if(guess[0]==actual[0]&&guess[1]==actual[1]&&guess[2]==actual[2])
{
System.out.println("Congradulations three matches in exact order!!! You win $1,000,000.");
return;
}
int count = 0;
for(int i=0;i<=2;i++)
{
for(int x=0;x<=2;x++)
{
if (actual[i]==guess[x])
{
count++;
guess[x]=-1;
break;
}
}
}
switch(count)
{
case 0:System.out.println("Sorry better luck next time."); break;
case 1:System.out.println("Good job. One match. You win $10."); break;
case 2:System.out.println("Awsome you made two matches. You win $100."); break;
case 3:System.out.println("Woohoo you matched all three. You win $1,000.");break;
}
}
public static void main(String args[])
{
aquireGuess();
generate
```
import java.util.Scanner;
public class Lottery {
static int guess[] = new int[3],actual[]= new int[3];
private static void aquireGuess()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Time to play the lottery please choose three numbers ranging from zero to nine.");
System.out.print("Please enter first number:");
guess[0] = keyboard.nextInt();
System.out.print("Please enter second number:");
guess[1] = keyboard.nextInt();
System.out.print("Please enter third number:");
guess[2] = keyboard.nextInt();
keyboard.close();
}
private static void generateLottery()
{
actual[0] = (int)(Math.random()*10);
actual[1] = (int)(Math.random()*10);
actual[2]= (int)(Math.random()*10);
}
private static void compare()
{
if(guess[0]==actual[0]&&guess[1]==actual[1]&&guess[2]==actual[2])
{
System.out.println("Congradulations three matches in exact order!!! You win $1,000,000.");
return;
}
int count = 0;
for(int i=0;i<=2;i++)
{
for(int x=0;x<=2;x++)
{
if (actual[i]==guess[x])
{
count++;
guess[x]=-1;
break;
}
}
}
switch(count)
{
case 0:System.out.println("Sorry better luck next time."); break;
case 1:System.out.println("Good job. One match. You win $10."); break;
case 2:System.out.println("Awsome you made two matches. You win $100."); break;
case 3:System.out.println("Woohoo you matched all three. You win $1,000.");break;
}
}
public static void main(String args[])
{
aquireGuess();
generate
Solution
Using method return types
Instead of relying on
Validating user input
For example (prompted by @KonradMorawski's comment):
Comparing arrays
Using other types for comparison
You may also want to consider using other types to compare the lottery numbers. For example, a plain integer works as well:
edit:
If you are on Java 7 and above, you should use
Instead of relying on
static int[] arrays, you should return the appropriate results from getting the user input and generating the lottery numbers so that their interaction is slightly clearer.Validating user input
Scanner.nextInt() throws an InputMismatchException when the next input is not parsable as an integer. You should have a method that handles that safely.For example (prompted by @KonradMorawski's comment):
private static int getInteger(Scanner scanner) {
System.out.println("Enter an integer:");
while (!scanner.hasNextInt()) {
System.out.println("Not an integer, please try again.");
scanner.next();
}
return scanner.nextInt();
}Comparing arrays
Arrays.equals(int[], int[]) can be used to compare two int[] arrays.Using other types for comparison
You may also want to consider using other types to compare the lottery numbers. For example, a plain integer works as well:
private static int getNumber(int a, int b, int c) {
return a * 100 + b * 10 + c;
}edit:
try-with-resourcesIf you are on Java 7 and above, you should use
try-with-resources for efficient handling of the underlying I/O resource:public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
// ...
}
}Code Snippets
private static int getInteger(Scanner scanner) {
System.out.println("Enter an integer:");
while (!scanner.hasNextInt()) {
System.out.println("Not an integer, please try again.");
scanner.next();
}
return scanner.nextInt();
}private static int getNumber(int a, int b, int c) {
return a * 100 + b * 10 + c;
}public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
// ...
}
}Context
StackExchange Code Review Q#109501, answer score: 6
Revisions (0)
No revisions yet.