Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: What's wrong with my 3 digit lottery?

1. ## What's wrong with my 3 digit lottery?

Greetings. I'm new here and I apologize in advance if I'm asking in the wrong area. Please move me if I'm wrong.

I am in an intro to java programming class and we have a bonus homework question. I missed the first homework because I couldn't access the homework. Each HW is worth 10 points and this bonus question is worth 3. The question is to generate a 3 digit lottery, use scanner to ask for a 3 digit guess, then the user wins \$10,000 if all the numbers match in the correct order, they win \$3,000 if all numbers match in any order, and they win \$1,000 if any one number matches.

I can't tell if the 3 in any order are right because I haven't guessed 3 correct numbers. I know the any one matching number is wrong, however... because it says, "Sorry, you win nothing" whenever I guess one or more right. (Edited...I was entering the numbers with spaces between them, as if they were individual scanner inputs. When I just enter a 3 digit interger, it works. Please tell me if my logic looks right for the 3 in any order) I've been staring at this code since Friday and any help you can provide would be appreciated more than you know. We haven't covered this yet in class....that's why it's a bonus question. Here's what I have:

```import java.util.Scanner;  //Scanner is in the java.util package
public class ThreeDigitLotteryHW {
public static void main(String[] args){

//create 3 digit random forced interger
int lottery = (int)(Math.random() * 1000);

Scanner input = new Scanner(System.in);

//Prompt the user to enter 3 intergers

int guess = input.nextInt();

//Turn guess interger into 3 individual intergers
int guessDigit1 = guess / 100;
int guessDigit2 = (guess / 10) % 10;
int guessDigit3 = guess % 10;

//Turn random 3 digit interger into 3 individual intergers
int lotteryDigit1 = lottery / 100;
int lotteryDigit2 = (lottery / 10) % 10;
int lotteryDigit3 = lottery % 10;

//Print converted random intergers
System.out.println("The lottery number is: " + lotteryDigit1 + " " + lotteryDigit2 + " " +lotteryDigit3);

//If user guesses all numbers in the correct order, they win \$10,000
if (guess == lottery)
System.out.println("You win \$10,000.");

//If 1st digit is correct and last two are correct in any order, user wins \$3,000
else if
((guessDigit1 == lotteryDigit1) &&
(guessDigit2 == lotteryDigit2 ||
guessDigit2 == lotteryDigit3) &&
guessDigit3 == lotteryDigit2 ||
guessDigit3 == lotteryDigit3)
System.out.println("You win \$3,000.");

//If 2nd digit is correct and 1st and 3rd are correct in any order, user wins \$3,000
else if
((guessDigit2 == lotteryDigit2) &&
(guessDigit3 == lotteryDigit3 ||
guessDigit3 == lotteryDigit1) &&
guessDigit1 == lotteryDigit1 ||
guessDigit1 == lotteryDigit3)
System.out.println("You win \$3,000.");

//If 3rd digit is correct and 1st and 2nd are correct in any order, user wins \$3,000
else if
((guessDigit3 == lotteryDigit3) &&
(guessDigit1 == lotteryDigit1 ||
guessDigit1 == lotteryDigit2) &&
guessDigit2 == lotteryDigit2 ||
guessDigit2 == lotteryDigit1)
System.out.println("You win \$3,000.");

//If 1st guess is correct in any lottery result, user wins \$1,000
else if
(guessDigit1 == lotteryDigit1 ||
guessDigit1 == lotteryDigit2||
guessDigit1 == lotteryDigit3)
System.out.println("You win \$1,000");

//If 2nd guess is correct in any lottery result, user wins \$1,000
else if
(guessDigit2 == lotteryDigit1 ||
guessDigit2 == lotteryDigit2 ||
guessDigit2 == lotteryDigit3)
System.out.println("You win \$1,000");

//If 3rd guess is correct in any lottery reult, user wins \$1,000
else if
(guessDigit3 == lotteryDigit1 ||
guessDigit3 == lotteryDigit2 ||
guessDigit3 == lotteryDigit3)
System.out.println("You win \$1,000.");

/*As we have used all other results, if no lottery results
* are guessed correctly, print, "Sorry, you win nothing"
*/
else
System.out.println("Sorry, You win nothing.");

//Close the input
input.close();```

2. ## Re: What's wrong with my 3 digit lottery?

Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

3. ## Re: What's wrong with my 3 digit lottery?

Sorry...thanks Greg

4. ## Re: What's wrong with my 3 digit lottery?

I can't tell if the 3 in any order are right because I haven't guessed 3 correct numbers.
In the future, rather than depending on luck, build test cases of known "random" lottery numbers. For example, rather than generate a real random lottery number, set the variable 'lottery' to a constant, like 123. Then see what happens when the user enters "guesses" that match all 3 in order, all 3 in any order, etc. From there, correct your logic to work correctly.

I take it you don't know arrays yet.

Since your update where you said "it works," I'm not sure what you're asking for help with. Please clarify.

5. ## Re: What's wrong with my 3 digit lottery?

Thanks for the reply Greg. No, I don't know arrays yet. I tried to just enter an interger for lottery instead of using random but got an error.\

I got it to work for guess one number in any spot. I think the logic is there for it to work with three numbers in any order, but IDK how to test it. That's what I'm wondering....will it work if I guess three numbers in any order.

I'm not asking for anyone to test it and try it for me, but could you please look at my first "if else" statements and tell me if it looks like it should work? I just want to be sure I get full credit for this assignment and as sad is it may be, I put two days' worth of work into this code so I just want someone's blessing before submitting it.

Thanks for taking the time to write to me and I hope someone can take the time to review my logic.

Thanks again, enjoy the rest of your evening.

6. ## Re: What's wrong with my 3 digit lottery?

tell me if it looks like it should work
With complicated if statements, it is easy to miss something.
Better to thoroughly test it.

7. ## The Following User Says Thank You to Norm For This Useful Post:

=Artie3881= (March 3rd, 2014)

8. ## Re: What's wrong with my 3 digit lottery?

Originally Posted by GregBrannon
In the future, rather than depending on luck, build test cases of known "random" lottery numbers. For example, rather than generate a real random lottery number, set the variable 'lottery' to a constant, like 123. Then see what happens when the user enters "guesses" that match all 3 in order, all 3 in any order, etc. From there, correct your logic to work correctly.

I take it you don't know arrays yet.

Since your update where you said "it works," I'm not sure what you're asking for help with. Please clarify.
Thanks Greg and Norm. I thought it was testing it would be much more complicated.

I just set the lottery number to 123 then tested all 3 cases and it worked!!! I had been forgetting to use a semicolon after entering a three digit interger for lottery. I'm thrilled that I got this b**** to work because I made a lot of mistakes along the way.

Thanks for taking the time to correct my mistaks when I first posted, Greg. I really think this forum will be a great resource for me. I plan to utilize it, this time knowing the proper way to post. I think I like programming, I get a real feeling of satisfaction when I get a program to run properly. This is the first program that stumped me for more than a couple of minutes.

Thanks again & enjoy your snow day if you're in the Northeast.

9. ## Re: What's wrong with my 3 digit lottery?

Good job, and congrats for figuring it out.

Yep, I'm home, waiting for the big snow to hit, staying off the roads.

Be well.

10. ## The Following User Says Thank You to GregBrannon For This Useful Post:

=Artie3881= (March 3rd, 2014)