當我使用這種編碼獲取用戶輸入時,我得到了債券錯誤。 noOfJudges
的用戶輸入是數組的長度。對於每一個法官進入和測試,看它是否是1到10之間 代碼
得分:爲什麼我得到這個「超出界限」的錯誤?
double [] scores = new double [noOfJudges];
System.out.print("Please enter the next score: ");
for(scoreEntry = 0; scoreEntry < scores.length; scoreEntry++)
scores[scoreEntry]=console.nextDouble();
System.out.println();
while((scores[scoreEntry] < MIN_SCORE)||(scores[scoreEntry] > MAX_SCORE))
{
System.out.print("Please reenter the score (must be between 1.0 and 10.0, in .5 increments): ");
scores[scoreEntry] = console.nextDouble();
System.out.println();
}
如果有人想完全很大,有沒有辦法來檢查,看是否有號碼是介於1到10之間,只有5個增量?
從我可以告訴,你得到的OutOfBoundsException因爲你scoreEntry元素尚未您最初的循環後復位。當您在while循環中調用分數[scoreEntry]時,該值大於score.length並超出界限。 – kamran619 2013-04-27 02:42:55