![]() |
| ||
| need help asap :) 1 Attachment(s) Here is the program requirement and I need the most simple method since i am a beginner and this is what i have so far import java.util.Random; class Lotto { public static void main (String[] args) { Random r = new Random(); int mynums[] = {6, 25, 31, 15, 42}; int lotto[] = {r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(49)+1, r.nextInt(42)+1}; int match; |
| ||
| Re: need help asap :) So what's the question? What are you having trouble with? Do you want me to write the application for you? |
| ||
| Re: need help asap :) 1 Attachment(s) Now this is what i've got but I am stuck and you can do anything to help me if u want id greatly appreciate it...My program is due tomorrow and I am trying really hard but not getting the concept |
| ||
| Re: need help asap :) Hi riabear123, Let's take a look at your Lotto.java file and try to figure out what it's doing. First, you import the Random class from java.util, because you need some way of generating random numbers. Then you set up the object-oriented overhead - the class and main method declarations. Then you get to the meat. You create a Random object called 'r'. You can think of this little guy as a worker who will perform certain jobs upon request. One job he can perform is called '.nextInt()' - the idea behind this job is given in the documentation for the Random class: nextInt(int n)That's a little confusing. What it means is, if you create a Random object called r, and then say something like: r.nextInt(5)- r will put the numbers 0, 1, 2, 3, and 4 into a hat and draw one out at random, then return that number. It doesn't include 5 (that's what the word "exclusive" means in the documentation) and it will pull any of those five numbers with the same probability (that's what the term "uniformly-distributed" means in the documentation). But don't take my word for it! After you've finished your program, try this out for yourself - it's the only way to learn how to code! I should also note that the name '.nextInt(n)' is a little misleading, since all it does is generate a random number. You are probably wondering "nextInt? Next to what?" Don't worry about that right now - it has to do with the internal design of how a Random object picks elements randomly, and it really has little to do with your program. So, keeping all that in mind, let's return to your program. After you create r, you create an array of integers called mynums[]. It has six slots, and those slots are filled by the numbers 6, 25, 31, 15, 42, and 36. Being a Lost fan, I prefer 4, 8, 15, 16, 23, and 42, but that's just me :cool: Then you create an integer variable called match, and set it equal to 0. Then you create six integer variables called lotto1, lotto2, lotto3, lotto4, lotto5, and lotto6. Each of these variables gets set equal to a random number - lotto1 through lotto5 get set equal to a random number between 1 and 50, and lotto6 gets set equal to a random number between 1 and 43. You get the random numbers by using successive calls to r.nextInt(n), where n is either 49 or 42, then adding one to shift the range. This is all clear so far, right? Now you enter a loop. The loop has an iterator called i, which increases by one after each iteration (cycle) of the loop. The loop only stops when i hits one million and one. In each iteration of the loop, you check the "lotto" variables against the entries in the "mynums" array. Whenever you find a match, you increase the match variable by one - except for the case when lotto1 is equal to mynums[0] - then, you increase match by two (I'm not sure if you really want to do this or not). Then you print out the value of match at this point. First, let's make an observation: the way the program is currently written, you never change the lotto variables, and you never change the mynums array. Thus, each iteration of the loop makes the exact same checks of the exact same numbers. Are you sure you want to do this? Why not make the lotto variables change inside the loop? Wouldn't that be more like a real sequence of lotteries? What's more, aren't you only supposed to win when all your numbers match up? The way you have it now, a match between any of your numbers and the corresponding lotto number will increase the value of match. Is this correct? Maybe you need to check all the numbers at once using the && operator, like so: if (condition1 && condition2 && ... && conditionN) {The thing in the parenthesis is often called a boolean expression - it's like an arithmetic expression, except that instead of resolving into a number, it resolves into either "True" or "False." Have you learned about these guys in class yet?I would like to help more, but I can't until you tell me what the assignment asks you to do. I hope that I have at least helped clear up some of the concepts. |
| ||
| Re: need help asap :) Thanks for taking time out to explain all that stuff..Here is what the program requirements are: CSC 101 Program 3 - Lotto It is said that if you buy a lot of lottery tickets it increases your chances of winning. Write a program that simulates buying a lot of tickets and add up how much money you would win. The lottery rules are like this. You pick 6 numbers, hoping to guess the numbers that will be drawn. When the drawing happens, there are 5 numbers drawn randomly between 1 and 49 inclusive, and then one more drawn from 1-42. The first 5 are unordered, but the last one is special and has to be an exact match to the last choice. Here is a table showing how much money you get for various numbers of matches between your picks and the numbers drawn. Match Payoff 5/1 Jackpot, let's say it's$35M 5/0 $100K 4/1 $5K 4/0 $100 3/1 $100 3/0 $7 2/1 $7 2/0 $4 1/1 $4 1/0 $1 0/1 $1 Your program should simulate buying a million tickets. You should assume that you use the same chosen numbers for all the tickets you buy. For each ticket, simulate the drawing and figure out how much money you won. Add this to a running total, and then buy the next ticket. At then end, say how much money you spent on tickets (1 ticket = 1 dollar) and how much money you won. Extra Credit Run the program using different amounts of tickets and different strategies for choosing your numbers and then make a recommendation for the best lotto playing strategy based on your programming results. This would need to be summarized in the comments section of Blackboard when you submit your program. PS. I'm still really lost :sad: |
| ||
| Re: need help asap :) Hi riabear123, It might be helpful to consider a simpler problem (in general, it almost always is easier to understand a programming problem when you reduce it to its simplest form). Imagine that you and I are playing a game - a very silly, pointless game. I write the numbers 0, 1, 2, 3, and 4 on five strips of paper and put them into a hat. You have a lucky number (let's say it's 3). The game is this: I pull a single strip of paper out of my hat. You guess that the number on that strip of paper is 3, your lucky number. If you are right, I give you a penny, then I put the strip of paper back in the hat. We play this exact same game a million times in a row. Well, that game is a lot like your lottery program. Me pulling a number out of my hat is similar to you calling r.nextInt(5). The lucky number (3) is one of the entries in your mynums[] array. And the number of pennies you have at the end is just like the match variable. Let's think about how we would code up this simple version, and then you can try to tackle the complicated one on your own. First off, what data does the program need to remember? It needs to remember three things: the lucky number, a random number, and the number of pennies you've won. Now, what does the program have to do? If the program is supposed to look like us playing a million of these games, it will have to loop a million times, right? And whatever happens in each game will happen in each iteration of the loop (in other words, inside the loop). So what happens in one of our games? First, I pick a number at random. Then, we check to see if this number matches your lucky number. If it does, we add one to the total number of pennies you own. Otherwise, we do nothing. And we do this same thing a million times over. At the end of the million games, we look at all the pennies you've won. That is the logic of our game. If we look at your code in Lotto.java, we see that it sort of looks like what I just described, except for two things: 1) You don't choose new random numbers inside the loop, like you should - you do it outside the loop. 2) You keep printing out the match variable inside the loop - that really ought to be outside the loop. Does this make sense? Please try to code up the simple version. I would like to help you more, but you have to meet me halfway. If you can't code up the simple version, try to think of the reason why, and ask a specific question. |
| ||
| Re: need help asap :) import java.util.Random; class Lotto { public static void main (String[] args) { Random r = new Random(); int mynums[] = {4, 8, 15, 16, 23, 42}; int match=0; int limit=5; for (int i=0; i<=1000000; i++) { int lotto1 =r.nextInt(49)+1; int lotto2 = r.nextInt(49)+1; int lotto3 =r.nextInt(49)+1; int lotto4 = r.nextInt(49)+1; int lotto5 = r.nextInt(49)+1; int lotto6 = r.nextInt(42)+1; } while (match<=limit) System.out.println("I have spent $1,000,000 on lottery tickets "); System.out.println("My total winnings are" + match); } } } } } } } can u help me compare my numbers to the random ones inside the loop |
| ||
| Re: need help asap :) i would like to use a switch statement b/c it would make it easier then comparing my numbers against the random numbers with if statements or a for loop but im not sure how |
| ||
| Re: need help asap :) Hi riabear123, I would really prefer that you try to get the simple version first, because your logic is still a little screwed up. For instance, why do you have that limit variable up top? And why did you insert a while loop at the bottom? And why are all those end-curly braces in there? Also, could you please use the CODE tags to delineate blocks of code in the future? It helps other forum members and myself read them. |
| ||
| Re: need help asap :) i had the limit variable up because i was going to have a counter, my teacher really cant teach and i dont know what the simple version is, i was to compare the numbers using a switch statement and then add the matches |
| All times are GMT -4. The time now is 8:34 am. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC