Java lottery program array
It only takes a minute to sign up. I am aware that the use of using namespace std is bad practice but my professor prefers that we use this way since it is a beginners course. The assignment was a two part assignment I have commented out the piece of the code that was unneeded for the second part.
Write a program that simulates a lottery. The program should have an array of 7 integers named winningDigitswith a randomly generated number in the range of 0 through 9 for each element in the array. The program should ask the user to enter 7 digits and should store them in a second integer array named player. The program must compare the corresponding elements in the two arrays and count how many digits match. For example, the following shows the winningDigits array and the player array with sample numbers stored in each.
There are two matching digits, elements 2 and 4. Modify the lab solution so that the player numbers are entered not from the keyboard, starting over at 35 from the file player. There's quite a few things I would change here.
Making sure that your program is correct is extremely important. As it stands this program is not correct as in does not meet the specification :. This comparison assumes that the arrays are sorted, which is not the case. So matching elements could be completely missed here. Sort the arrays first before comparing them or consider a different data structure to store the results from generating the numbers. There's commented out code in the source, this is almost always a bad sign.
Keep code changes tracked by version control, not commented out blocks. I personally always use braces as does the coding standard we go by at my work but whatever you do you need a tab here:. Other people have commented on this, but generally speaking the only time you would really want to do this is when you are making some small throwaway program for testing a concept or making an example or similar.
You probably don't want to do it in any production code. Instead of these I would use std::array for both of these as the length is both fixed and known at compile time. Were then lengths not known then std::vector would most likely be the best choice. These become:. Now this ends up being the same type in a bunch of different places so a typedef will help make things easier:.
An additional benefit of the typedef is that if you do change the type later it will be easier to do so. Additionally rand gets worse when you take the modulus of it statistically speaking. The output code definitely violates the "don't repeat yourself" principle. Success has posted a great answer that outlines some of the things you can do to reduce that duplication.
Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.
Sorry for taking a while to get back to you. Been a busy couple days. As requested, here is the complete program. Note that this isn't entirely your original program.
I removed a couple things and added a few more in. Perhaps most importantly, I changed the function that I gave you previously. I originally answered your question to try to only give you the four digit match but if I am writing out the whole thing it makes more sense to generalize a bit.
Because your lottery awards are based on the number of matching digits, it makes a lot of sense to make a function that counts how many digits match.
This simplifies your if-else code a whole lot. Looks like I did not understand correctly. I believe there are multiple ways to get a given digit of a number but I think this is the easiest to think about. You divide by to shift the thousands digit into the 1's place then use the modulus operator to get rid of anything that isn't in the 1's place.
This leaves you with nothing but the fourth digit of the number. If I understand correctly, your difficulty is with matching different ordered four digit numbers.
The most obvious way to do this is to just check all possible orderings of the four digits; there are only 15 where they are not exactly equal. However, typing out 15 conditions is prone to errors and just plain boring. It would be twice as boring and tedious if you instead needed to do this with five digits.
Here is a function that avoids this by using String instead of int. It repeatedly checks the first character of the guess and checks if the character is in the answer. It then removes that matching character from the answer to avoid a case like andwhere every character in the guess is also in the answer but they do not match.
Learn more. Asked 3 years, 6 months ago.
Active 3 years, 6 months ago.Ive been really struggling with my online Java class and my teacher just drops this on me, any help what so ever would be greatly appreciated! Write a class called PowerBall that simulates the Power Ball lottery game. The class should have an array of integers representing the 5 winning picks for the white balls numbered 1 to 59 no duplicatesan integer representing the winning power ball number numbered 1 through 35, and an integer that specifies the jackpot for the game.
The user picks are compared against the numbers and power ball generated by the power ball game. Use the chart below to determine the payout. Your PowerBallConsole program should create a new PowerBall instance and ask the user to enter their 5 picks plus a power ball number. Use the following table to determine the payout:. EDIT:: This is going to remove all the plus signs in my replies.
EDIT:: I included duplicate checking, as well as the final outputs. All that i think is missing now is putting in user input and duplicate checking user input Let me know if this is of any help. I over commented it thou just to help explain it. The comments are not needed. And i set the max balls to 6, and powerball to Create a program called PowerBallConsole that includes a main method to test your PowerBall implementation. Answer Save. EDIT:: The user input does not check to see if it's within the ranges provided.
Still have questions? Get your answers by asking now.Joe Niksa wrote: when I change the number, the error changes to the new one I put in.
An array identifier the name of the array is essentially a memory address of where the array begins in memory. For an int array called foo, you have 5 elements on the array. The actual indices of the elements range from Forum: Beginning Java. Joe Niksa. I am coming across an error in my code. It prints, "Exception in thread "main" java. I understand it has something to do with the number of counters in the array because when I change the number, the error changes to the new one I put in.
I'm really confused on how arrays work so any help would be greatly appreciated. Anayonkar Shivalkar. I like If you are not aware of how array works, please check out relevant sections in any data structure book of your choice. I hope this helps. Tim Hagberg. Arrays are zero indexed, which means the first element on an array is at index 0. This is because of how pointer arithmetic works. I hope this clears up a little of how arrays work for you.
Jeff Verdegan. Tim Hagberg wrote: This is because of how pointer arithmetic works. Historically, yes, but not relevant for the Java language per se.
In any given JVM's implementation of a Java language array, probably, but not actually required by the spec as far as I know. In the Java language, not at all, as there is no such thing as a memory address.
The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.
I am designing a program for my class that is supposed to simulate a lottery game. I am supposed to design a method that generates random lottery numbers, a method that asks and stores the user for their number choices, a method that compares the arrays to find how many numbers are the same, and then I am supposed to call them all back up to the main method, and create my output statement that contains some if statements that determine which prize is awarded for the particular amount of matches.
Please excuse my spacey coding style and any blatant mistakes that I have made. Any tips,advice, solutions, or if you notice anything wrong with what I have please let me know. Keep in mind that randNum. You can use a for loop to assign the random numbers to the lotteryNumbers array easier. Also, you should make sure that the random lottery numbers don't repeat. In your compareArrays function, just put one return sameNum call after the outermost for loop, otherwise it won't update with the correct number of matching numbers.
You need to give compareArrays the correct parameters userDigits and lotteryNumbers and set sameNum equal to this result. Instead of multiple if s use switchplus do not repeat the whole print statement, only assign the part that differs:.
Subscribe to RSS
Learn more. Asked 5 years, 4 months ago. Active 5 years, 4 months ago. Viewed 12k times. Here is what I have thus far import java. Drewmeister96 Drewmeister96 11 1 1 gold badge 1 1 silver badge 4 4 bronze badges.
So have you found any of the answers useful? Active Oldest Votes. Thank you, that really helped. Those issues with the parameters were throwing everything off, but I got it all working now. Thanks so much! Glad I could help! If you are satisfied with my answer, would you kindly mark it as "accepted"? I've updated your code with the changes you'll need. Random; import java. Okay thank you. I am now getting an output with the lottery numbers and the user entry numbers, but the matches keep appearing as 0.
You're returning after the first if statement in the inner loop as well as in the outer loop, so you'll never get through your arrays.
You know, you could have left the generation methods as they were, not sure why they didn't work. You're returning an int from them instead of void, but that really shouldn't matter. The argument would be the reference to the array, so updates to that reference should persist as long as the reference stays in scope.Hi there, as one of my first programs in java, I thought I will do a simple!
Subscribe to RSS
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. Scanner; it says thatthis doesn't correspond to the specified code style?!
Any idea what I have done wrong? Please take in consideration that as I said I am very new to Java thanks. Thanks I will make the amendments, try and post back the results.
I would like to clarify one thing if I may: I thought that a variable initialization in java isn't not mandatory,but I seem to understand from your amendments that it is Fair enough for the other erros, all things I should have been more careful with, thank you very much for pointing them out, much appreciated.
This compiles ok but there is a problem. In the console when I input the numbers, the number I input gets printed out straight away, so I end up with the same number twice. Maybe a screenshot clarifies things. The first value being printed is the user entered value from key board which is the the input from user to program.
Scanner; import java. Ah, what a donkey I am! Sorry, didn't think about that! Getting Started: Have something to contribute to this discussion?
Please be thoughtful, detailed and courteous, and adhere to our posting rules. Edit Preview. H1 H2.Leave a Reply. Bloggerized by DheTemplate. Pages Home. Sunday, January 15, A lottery requires that you select six different numbers from the integers 1 to Write a program to do this for you and generate five sets of entries. Share to Twitter Share to Facebook. Newer Post Older Post. Subscribe to: Post Comments Atom. Popular Recent Archives.
Total Pageviews. Popular Posts of This Week Program to find whether entered character is a vowel, constant, number or a special character.
Here is Program to find whether entered character is a vowel constant number or a special character import java. This is implem Run Excel Macro from Java. This is a java code snippet which will run VB script. The excel macro called " Java Class to Calculate the Volume of Box. Java Program to Generate the Lottery Number between 1 to Write a program to do this for you and generate five s Printing Table In java using While Loop.
This code snippet is used for automatic splitting of files conatining large content into smaller text files with specific number of recor UDP uses a simple transmission model without implicit handshaking dialogues for providing reliability, ordering, or data integrity. Alphanumeric String Sorting using Java.
Alphanumeric is a combination of alphabetic and numeric characters sometimes shortened to alphameric. In computing, the alphanume The diameter of the sun is approximatelymiles. The diameter of Earth is approximately miles.
Use the methods in the class Ma Powered by Blogger.Learning Java: Part 8: Generating Random Numbers
To demonstrate the difference between public and p To Automatic type conversions apply to overloading Java class that defines an integer stack that can Java Program to Demonstrate the bitwise logical op To Demonstrate the Increment and Decrement Operat Java Program to Demonstrate Lifetime of a Variable Java Program to Demonstrate block scope.