GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Introduction_to_Java_Programming / Chapter03 / Lottery.java /
No definitions found in this file.
- Go to file T
- Go to line L
- Go to definition R
- Copy path
|import java.util.Scanner ;|
|public class Lottery <|
|public static void main ( String  args ) <|
|// Generate a lottery|
|int lottery = ( int )( Math . random() * 100 );|
|// Prompt the user to enter a guess|
|Scanner input = new Scanner ( System . in);|
|System . out . print( ” Enter your lottery pick (two digits): ” );|
|int guess = input . nextInt();|
|// Get digits from lottery|
|int lotteryDigit1 = lottery / 10 ;|
|int lotteryDigit2 = lottery % 10 ;|
|// Get digits from guess|
|int guessDigit1 = guess / 10 ;|
|int guessDigit2 = guess % 10 ;|
|System . out . println( ” The lottery number is ” + lottery);|
|// Check the guess|
|if (guess == lottery)|
|System . out . println( ” Exact match: you win $10,000 ” );|
|else if (guessDigit2 == lotteryDigit1|
|&& guessDigit1 == lotteryDigit2)|
|System . out . println( ” Match all digits: you win $3,000 ” );|
|else if (guessDigit1 == lotteryDigit1|
||| guessDigit1 == lotteryDigit2|
||| guessDigit2 == lotteryDigit1|
||| guessDigit2 == lotteryDigit2)|
|System . out . println( ” Match one digit: you win $1,000 ” );|
|System . out . println( ” Sorry, no match ” );|
- Copy lines
- Copy permalink
- View git blame
- Reference in new issue
- © 2020 GitHub, Inc.
- Cookie Preferences
- Contact GitHub
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.原书样例代码和习题解答代码. Contribute to guodongxiaren/Introduction_to_Java_Programming development by creating an account on GitHub.
simple lottery program
Hi there, as one of my first programs in java, I thought I will do a simple (!) lottery program. needless to say it doesn’t compile, and after looking at it for quite awhile trying to unsuccessfully spot the error (I am not sure how to debug in netbeans, any advice on that very welcome too of course) I thought that it is time for some help. here’s the program (compiled with netbeans):
SO the idea behind this is that the computer generates 6 random number in the range of 1 to 49, and store them in an array, the user inputs his own 6 guesses, stores them into an array, then each entry is compared to what the computer has stored and for each right guess the counter is incremented by 1. If the counter is 6 then you win, if not you lose.
Now, netbeans is giving me some strange errors and warnings:
1) with this i mport java.util.Scanner; it says thatthis doesn’t correspond to the specified code style.
2)here int randomNums = new Random().next(49) + 1; it tells me that next(Int) has protected access, whatever that means;
3) at the else level it says: ‘else’ without ‘if’. ehm?!
4) at c ounter+=1; it says variable counter might not have been initialised
5) the last > says “class, inteface or enum expected”
Any idea what I have done wrong? Please take in consideration that as I said I am very new to Java