wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Making Decisions Lottery application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Making Decisions Lottery application" Watch "Making Decisions Lottery application" New topic
Author

Making Decisions Lottery application

David Godwin
Greenhorn

Joined: Feb 14, 2012
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.


[Thumbnail for Lottery Output example.jpg]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Godwin
Greenhorn

Joined: Feb 14, 2012
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

Joined: Jan 10, 2002
Posts: 61298
    
  66

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

Joined: Oct 13, 2005
Posts: 39044
    
  23
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

Joined: Feb 14, 2012
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

Joined: Nov 09, 2011
Posts: 1025
    
    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

Joined: Feb 14, 2012
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

Joined: Jan 18, 2012
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

Joined: Mar 12, 2011
Posts: 808
What about the pseudocode?
David Godwin
Greenhorn

Joined: Feb 14, 2012
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

Joined: Feb 14, 2012
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

Joined: Feb 14, 2012
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

Joined: Mar 12, 2011
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

Joined: Feb 14, 2012
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

Joined: Jan 18, 2012
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.
 
GeeCON Prague 2014
 
subject: Making Decisions Lottery application