2013-04-27 26 views
0

當我使用這種編碼獲取用戶輸入時,我得到了債券錯誤。 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個增量?

+0

從我可以告訴,你得到的OutOfBoundsException因爲你scoreEntry元素尚未您最初的循環後復位。當您在while循環中調用分數[scoreEntry]時,該值大於score.length並超出界限。 – kamran619 2013-04-27 02:42:55

回答

0

應使用一個局部變量來的for循環或復位scoreEntry,因爲在的for循環結束scoreEntry等於陣列的長度,這不是有效的索引...

0

後的循環結束,

System.out.println(scoreEntry);

潛藏你的答案:)

最佳,

ANKIT

0

你必須忘了一對{}

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(); 
    } 
}