Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Making Decisions Lottery application

David Godwin
Greenhorn
Posts: 8
Hello,

I am stuck when it comes to writing this code. Everything I have had to do was an easy walk through, but know we are told to use psedocode to begin this before we actually write the application.

Create a lottery game application. Generate three random numbers, each between 0 and 9. Allow the user to guess three numbers. Compare cah of the user's guesses to the three random numbers and display a message that includes the user's guesses, the randomly determined three-digit number, and the amount of money the user has won as follows:

any one matching: 10\$
two matching: 100\$
three matching, not in order: 1,000\$
three matching: 1,000,000\$
no matches: 0

Make certain that your application accommodates repeating digits. For example, if a user guesses 1, 2, and 3, and the randomly generated digits are 1, 1, and 1, do not give the user credit for three correct guesses - just one.
Lottery Output example.jpg

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65279
95
Welcome to the Ranch.

Where's your code? What specific problems are you having with it? I'm sure you're not here to try and get someone else to write the code, right?

David Godwin
Greenhorn
Posts: 8
Your on to me lol. I'm overwhelmed with this one. But as soon as I write some more if...else and && || . I will post this mess.

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65279
95
• 1
Breathe deeply a few times and tackle it a piece at a time. Like any other project, the whole shebang scan overwhelm you. Focus on each part until you get it working -- pretty soon the whole thing comes together.

When you post code, be sure to UseCodeTags to keep it readable.

Campbell Ritchie
Sheriff
Posts: 50770
83
• 1
As far as possible, you ought not to create a new Random object in the method. You should have the Random object as a field of the class initialised in the constructor, and as far as possible use the same Random object throughout. That is to obviate the risk (much smaller since Java5 than it was before) that two Random objects will return the same series of numbers.

David Godwin
Greenhorn
Posts: 8
Ok, I have written the output to the code so that it will display. My next step would be to input the random after the number choices. I also should be able to show invalid input if the user inputs a letter or number other than 0-9 so that it shows that they made an error. I will probably work on this fairly quickly, but the real trouble is awarding the prizes when the user matches the randoms and so on...

James Boswell
Bartender
Posts: 1051
5
Hi David

First and only comment on your code. Push the code you have written so far into a method called readUserInput and call that from your main method which should be kept short in length.

David Godwin
Greenhorn
Posts: 8
Ok I see what you mean by that... I just read the link about posting code and ShowSomeEffort . I will go back in my book and try to find out about the way we limit the numbers. As of right now I can only come up with for

I still seem to be able to write letters and int when I run it.

Ryan Sykes
Ranch Hand
Posts: 58
I would personally recommend storing each user input number in an int[] array or a List rather than in separate variables. If you do the latter, you will have to hard-code all comparison logic using individual variable names. Arrays or Lists would make it much easier to extend the program to a situation where you had more than 3 digits.

Also, read the methods for Random here: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html

That should help you figure out how to generate the random numbers internally.

dennis deems
Ranch Hand
Posts: 808

David Godwin
Greenhorn
Posts: 8
I guess it is good practice to draw out the psedocode, kind of like directions for yourself. They said that programmers will use flowcharts, but we don't have the software, so I will have to write it out by hand.

David Godwin
Greenhorn
Posts: 8
I have written the Scanner for the three numbers that I can input for three different println lines. I made them only be int 0-9. But if user types a letter or symbol Java errors out. Would I have to include the input in my syntax for a variable so that it isn't true? A Boolean maybe?

David Godwin
Greenhorn
Posts: 8
By the way, this forum is really keeping me motivated. It is making me really try to show everyone here that I can walk myself through this.

I thought I was alone out there with these questions. It is very nice to know that I can push myself to get these done without just walking away.

dennis deems
Ranch Hand
Posts: 808
David Godwin wrote:I guess it is good practice to draw out the psedocode, kind of like directions for yourself. They said that programmers will use flowcharts, but we don't have the software, so I will have to write it out by hand.

And so you're writing the code first??

David Godwin
Greenhorn
Posts: 8
I have a few sketches by hand, but this is just Introduction to programming. I see ahead that I can work on arrays and loops further on in the text, but I have to work this out with if if...else && and ||. We covered switch method and Conditional and NOT operators.

Ryan Sykes
Ranch Hand
Posts: 58
David Godwin wrote:I have a few sketches by hand, but this is just Introduction to programming. I see ahead that I can work on arrays and loops further on in the text, but I have to work this out with if if...else && and ||. We covered switch method and Conditional and NOT operators.

That still doesn't negate the use of pseudocode. If anything, you should focus more on writing your pseudocode that explains the logic behind how you plan on tackling this assignment, rather than worrying about the syntax. If the logic is sound, the rest becomes a lot easier. So instead of posting code, why don't you focus on posting your pseudocode explaining the logic involved in completing this assignment. Assume that you have already parsed each of the 3 numbers from the user input and then try to figure out the pseudocode for getting the necessary output.

Also, if you haven't done loops yet, then checking the input for invalid input is not going to be very fruitful. As it stands, even when you detect a wrong input (number not between 0-9), the program proceeds to take the next input. You might be better off printing an Error message and using System.exit(0) to terminate the program. You could use loops to continue attempting an input till it is acceptable...but this is not so important. As I suggested...focus on the more important tasks at hand at try to write out the pseudocode here. If you are stuck in a specific section, folks might be able to assist you.