[英]Why am I getting this “Out of Bounds” error?
当我使用此编码来获取用户输入时,出现了键外错误。 noOfJudges
的用户输入是数组将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之间并且仅以0.5为增量?
您应该在for循环或reset scoreEntry
使用局部变量,因为在for循环的 scoreEntry
等于数组的长度,这不是有效的索引...
for循环结束后,
做一个System.out.println(scoreEntry);
这就是你的答案:)
最好,
安奇
您必须忘记一对{}
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.