這裏我的代碼至今:計數頻率在一個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發生...「?我覺得這不太可能。 –