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 之间并且仅以 0.5 为增量?

4

3 回答 3

0

您应该对for 循环或 reset使用局部变量scoreEntry,因为在for 循环 scoreEntry结束时等于数组的长度,这不是有效的索引...

于 2013-04-27T02:45:21.827 回答
0

for循环结束后,

做一个System.out.println(scoreEntry);

这就是你的答案:)

最好的,

Ankit

于 2013-04-27T02:57:14.120 回答
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();
    }
}
于 2013-04-27T03:27:19.123 回答