creating a small lottery application, java
I'm a java noob and have been looking all over for some help with how to create this lottery application. I read some examples on this site but some of the code was a bit too advanced for me to actually implement(I'm not sure how O(n) works?) so I thought I'd asked my question here while I keep looking for the answers.
I need to create a really simple application that generates 3 random numbers and compares them with 3 user guesses. The numbers are all single digit but they can be repeating. For example the actual numbers might be 1, 1, 1 and the user may have guessed 1, 2, 3 but the prize money would only match the 1 the user guessed with any 1 in the actual number for a single match.
I just need to compare 2 arrays I guess and account for repeating digits. I tried creating nested if statements but there's 20 or so premutations and it gets out of hand reall开发者_运维问答y fast. What would a pro do?
Thanks for any help
Well, to see if an array contains at least one element of another array, I'd do the following:
int[] actualNums;
int[] guesses;
for (int i = 0; i < actualNums.length; i++) {
for (int j = 0; j < guesses.length; j++) {
if (actualNums[i] == guesses[j]) {
// if you are here, you have found at least one match.
}
}
}
If I were you, I'd use the java.util.Random
class. In your case, you'd make it work like this. Start with importing the Random
class.
import java.util.Random;
Then make an array of random integers 0-9.
int[] randomNums;
Random generator = new Random(); //instance of a Random object
for(int i = 0; i < 3; i++) {
randomNums[i] = generator.nextInt(10); //nextInt method gives int 0-9
}
For the user-inputted array, follow a similar process except using arguments. Then, you can iterate through the arrays together and see if they match. If you're doing what I think you're doing, you'd have to use nested for
loops (two of them, to be precise).
This is what I think you will look for in your core code
public class Lottery{
public boolean isWinner(ArrayList<?> user, ArrayList<?> lotto){
if(user == null || lotto == null)
return false;
if(user.size() == 0 || lotto.size() == 0)
return false;
return lotto.containsAll(user);
}
}
This uses ArrayLists of any type, and uses the existing containsAll() functionality.
This would be easy to setup
Lottery lottery = new Lottery();
ArrayList<Integer> userOne = new ArrayList<Integer>();
ArrayList<Integer> lotto = new ArrayList<Integer>();
userOne.add(/*Some int value*/); //do this XX number of times as needed
lotto.add(/*Some int value*/); //do this XX number of times as needed
lottery.isWinner(userOne, lotto);
edit This will work in the case of lotto={3,3,3} user={1,3,8} but breaks if you swap numbers: lotto={1,3,8}, user={3,3,3}. If you know the sizes will always be the same you can try
if(lotto.containsAll(user)){
return user.containsAll(lotto);
}
return false;
You should put your numbers into a collection class like List
(duplicates allowed) or Set
(no duplicates here). Then you can use the existing methods contains()
or equals()
to look for matches.
精彩评论