这里我的代码至今:计数频率在一个HashSet的一些已经被拾取
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");
}
}
所以基本上,我所做的就是创建一个HashSet,并把在6张随机数。在我最近的2个for-loops中,我试图计算出多少次绘制哪个数字并打印出来。问题在于,无论出于何种原因,打印出来时都以第三个字段开头。有谁知道为什么?
如果你不是来开始你的最后'for'循环在0? – khriskooper
如果我开始第一个for循环与0它会给我7个数字而不是6,我不想有。尝试使用最后一个for循环,它仍然在字段3开始。这里有一个截图:http://puu.sh/whYKh/46b2172585.png – TheSTARplow
当你说在字段3开始,你的意思是打印“3发生...“并跳过”1发生...“和”2发生...“?我觉得这不太可能。 –