[英]Why am I getting this “Out of Bounds” error?
I am getting an out of bonds error when I use this coding to get user input. 当我使用此编码来获取用户输入时,出现了键外错误。 The user input for
noOfJudges
is how long the array will be. noOfJudges
的用户输入是数组将noOfJudges
多长时间。 For each judge a score is entered and tested to see if it is between 1 and 10. 为每个法官输入分数并进行测试,看分数是否在1到10之间。
Code: 码:
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();
}
If anyone wants to be totally great, is there a way to check to see if a number is between 1 and 10 and only in .5 increments? 如果有人想成为一个伟大的人物,是否有一种方法可以检查数字是否介于1到10之间并且仅以0.5为增量?
您应该在for循环或reset scoreEntry
使用局部变量,因为在for循环的 scoreEntry
等于数组的长度,这不是有效的索引...
After the for loop ends, for循环结束后,
do a System.out.println(scoreEntry);
做一个
System.out.println(scoreEntry);
Therein lies your answer :) 这就是你的答案:)
Best, 最好,
Ankit 安奇
You must forgot a pair of {}
您必须忘记一对
{}
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.