Reputation: 41
First post on here because I'm frustrated. The assignment is to create a program in which 3 random numbers from 0-9 are generated, and where the user will input 3 numbers. If 1 number matches - the user wins $10, if 2 - $100, if 3 - $1000, and if all 3 and in perfect order - $1000000. For some reason, even if none of the numbers match up, it will award me with $10, and there are other problems.
I know there must be a simpler way. This is my first java course so I'm still a beginner but this is the code I came up with:
public class TheLottery
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("enter a number from 0-9");
int num1 = input.nextInt();
System.out.println("enter a second number from 0-9");
int num2 = input.nextInt();
System.out.println("enter a third number from 0-9");
int num3 = input.nextInt();
Random r=new Random();
int a1=r.nextInt(10);
Random r1=new Random();
int a2=r.nextInt(10);
Random r2=new Random();
int a3=r.nextInt(10);
System.out.println("The winning numbers are "+ a1 + a2 + a3);
boolean winning1=true;
boolean winning2=true;
boolean winning3=true;
if (num1 == a1 || num1 == a2 || num1 == a3)
winning1 = true;
else
winning2 = false;
if (num2 == a1 || num2 == a2 || num2 == a3)
winning2 = true;
else
winning2 = false;
if (num3 == a1 || num3 == a2 || num3 == a3)
winning3 = true;
else
winning3= false;
if(winning1==true && winning2==true && winning3==true && num1 == a1 && num2==a2 && num3==a3)
System.out.println("YOU'RE A MILLIONAIRE!");
else if (winning1==true && winning2==true && winning3==true)
System.out.println("You win 1,000 dollars!");
else if ((winning1==true && winning2==true) || (winning1==true && winning3==true) ||(winning2==true && winning3==true))
System.out.println("You win 100 dollars!");
else if (winning1==true || winning2==true || winning3==true)
System.out.println("You win 10 dollars!");
else
System.out.println("You lose!");
}
}
Upvotes: 0
Views: 2059
Reputation: 298
Since winning1
defaults as true, you will always have $10 due to this line of code:
if (num1 == a1 || num1 == a2 || num1 == a3)
winning1 = true;
else
winning2 = false;
winning1
never can equal false. Also, I would use arrays and for loops. For example:
Random r=new Random();
int[] winningNumbers = new int[2];
for (int i = 0; i<2; i++) winningNumbers[i] = r.nextInt(10);
Scanner input = new Scanner(System.in);
int[] numbers = new int[2];
for (int i = 0; i<2; i++) number[i] = input.nextInt();
I'm not going to write all the code, but you can research arrays and for loops. Once you learn about them, try to redo this program.
Upvotes: 0
Reputation: 4318
The first problem has been pointed out by @Adam S. You are assigning the wrong boolean variable at the first attempt check.
It would be easier if from the start you keep track of the wins, instead of which attempts were successful.
int wins = 0;
if (num1 == a1 || num1 == a2 || num1 == a3)
wins++;
if (num2 == a1 || num2 == a2 || num2 == a3)
wins++;
if (num3 == a1 || num3 == a2 || num3 == a3)
wins++;
if(wins == 3)
if(num1 == a1 && num2==a2 && num3==a3)
System.out.println("YOU'RE A MILLIONAIRE!");
else
System.out.println("You win 1,000 dollars!");
else if (wins == 2)
System.out.println("You win 100 dollars!");
else if (wins == 1)
System.out.println("You win 10 dollars!");
else
System.out.println("You lose!");
Upvotes: 2
Reputation: 11888
You are assigning the wrong variable to false in your conditional. It should be
if (num1 == a1 || num1 == a2 || num1 == a3)
winning1 = true;
else
winning1 = false;
You are also declaring new random variables but never using them. But the above is why you were being awarded $10 even if you don't match any numbers.
Upvotes: 0