2013-04-27 30 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(); 
    } 
}