954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with Lottery program

Im new to java and just trying to figure out the basics. i cannot figure out what is wrong with my program. Any suggestions on whats wrong?

4import java.util.Scanner;
5
6public class Lottery
7{
8	public static void main(String[] args)
9	{
10	//Generate random lottery numbers
11	int lottery = (int)(Math.random() * 1000);
12	System.out.printf("%d" , lottery);
13
14	//Create Scanner
15	Scanner input = new Scanner(System.in);
16	System.out.print("Enter your lottery pick (three digits): ");
17	int guess = input.nextInt();
18
19	//Determine whether or not the entered guess is some kind of match
20	if(guess == lottery)
21		System.out.println("Exact mactch: you win $10,000");
22	else if (guess % 100 == lottery / 100 &&
23			guess / 100 == lottery % 100 &&
24			guess % 10 == lottery / 10 &&
25			guess / 10 == lottery % 10)
26		System.out.println("Match all digits: you win $3,000");
27	else if (guess % 100 == lottery / 100 ||
28			guess % 100 == lottery % 100 ||
29			guess / 100 == lottery / 100 ||
30			guess / 100 == lottery % 100 ||
31			guess % 10 == lottery / 10 ||
32			guess % 10 == lottery % 10 ||
33			guess / 10 == lottery / 10 ||
34			guess / 10 == lottery % 10 )
35		System.out.println("Match one digit: you win $1,000");
36	else
37		System.out.println("Sorry, no match");
38	}
39}
Andrewsc1
Newbie Poster
9 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

You should probably explain what it is doing wrong, what you expect it to do, and any errors you receive, rather than expecting people here to pick through it and play "find the problem with this".

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

sorry. well the program is supposed to tell if you won the randomly generated lottery number. i used the scanner class so that the user could enter in a number. i extracted digits from the lottery class and guess class using the "%" and "/". i cannot figure out why i cannot get the "Match all digits: you win $3,000" to appear in the console. so in all reality im just looking into why lines 22-25 are not extracting the digits.

Andrewsc1
Newbie Poster
9 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Most likely because the formula you implemented to determine "match all digits" doesn't do that at all. Those are rather odd relationships that you are expecting between the division and mod results. You need to break the numbers into their components before you make the comparison checks.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

thank you for the help.

Andrewsc1
Newbie Poster
9 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You