Reputation: 13
Here my code until now:
public void lottoGame() {
HashSet<Integer> dups = new HashSet<Integer>();
for(int howMany = 1; howMany <= 6; howMany++) {
int lottoNumber = lottoNum.nextInt(49) + 1;
while(dups.contains(lottoNumber)) {
lottoNumber = lottoNum.nextInt(49) + 1;
}
dups.add(lottoNumber);
randomLotto[howMany] = lottoNumber;
System.out.println(lottoNumber);
}
for(int counter : dups) {
numberCount[counter]++; //to store the counting of random numbers
}
for(int counting = 1; counting < numberCount.length; counting++) {
System.out.println(counting + " occurs " + numberCount[counting] + " times");
}
}
So basically, what I did was create a hashset and put in 6 random numbers. In my last 2 for-loops I'm trying to count how often I draw which number and print that out. The problem is, it starts with the 3rd field when printing out, for whatever reason. Does anyone know why?
Upvotes: 0
Views: 151
Reputation: 1258
I would suggest using map it is much simpler here look at this example
public static void lotoGame() {
Random rand = new Random();
Map<Integer, Integer> loto = new HashMap<>();
while (loto.keySet().size() < 6) {
int lottoNumber = rand.nextInt(49) + 1;
loto.compute(lottoNumber, (key, value) -> value == null ? 1 : ++value);
}
loto.forEach((key,value)->{
System.out.println(key + " occurs " + value + " times");
});
}
Upvotes: 0
Reputation: 676
Here is the example you gave. The output I have is correct. Verify your settings or other code you might not show us.
Look at : Is there a limit on the maximum of number of lines which can be printed to console by BlueJ?
class Test
{
public void lottoGame()
{
int[] randomLotto = new int[50];
int[] numberCount = new int[50];
Random lottoNum = new Random();
HashSet<Integer> dups = new HashSet<Integer>();
for (int howMany = 1; howMany <= 6; howMany++)
{
int lottoNumber = lottoNum.nextInt(49) + 1;
while (dups.contains(lottoNumber))
{
lottoNumber = lottoNum.nextInt(49) + 1;
}
dups.add(lottoNumber);
randomLotto[howMany] = lottoNumber;
System.out.println(lottoNumber);
}
for (int counter : dups)
{
numberCount[counter]++; //to store the counting of random numbers
}
for (int counting = 1; counting < numberCount.length; counting++)
{
System.out.println(counting + " occurs " + numberCount[counting] + " times");
}
}
public static void main(String[] args)
{
new Test().lottoGame();
}
}
Output :
43
27
38
30
14
8
1 occurs 0 times
2 occurs 0 times
3 occurs 0 times
4 occurs 0 times
5 occurs 0 times
6 occurs 0 times
7 occurs 0 times
8 occurs 1 times
9 occurs 0 times
10 occurs 0 times
11 occurs 0 times
12 occurs 0 times
13 occurs 0 times
14 occurs 1 times
15 occurs 0 times
16 occurs 0 times
17 occurs 0 times
18 occurs 0 times
19 occurs 0 times
20 occurs 0 times
21 occurs 0 times
22 occurs 0 times
23 occurs 0 times
24 occurs 0 times
25 occurs 0 times
26 occurs 0 times
27 occurs 1 times
28 occurs 0 times
29 occurs 0 times
30 occurs 1 times
31 occurs 0 times
32 occurs 0 times
33 occurs 0 times
34 occurs 0 times
35 occurs 0 times
36 occurs 0 times
37 occurs 0 times
38 occurs 1 times
39 occurs 0 times
40 occurs 0 times
41 occurs 0 times
42 occurs 0 times
43 occurs 1 times
44 occurs 0 times
45 occurs 0 times
46 occurs 0 times
47 occurs 0 times
48 occurs 0 times
49 occurs 0 times
Upvotes: 1