尝试从文本文件填充数组,但需要包含 (0-100)

Trying to populate an array from a text file, but needs to be inclusive (0-100)

我制作了一个程序,它从文件中获取输入(分数)并输出所有分数,以及平均分、最高分和最低分。但是,我正在努力排除不在 0-100 范围内的分数(排除意味着打印到监视器而不是实际输出文件),这也会混淆我得到的结果。我的 if/else 条件似乎根本没有做任何事情。这是我的变量,以及我必须填充数组的循环。感谢任何帮助,谢谢!

   int sum = 0; // sum of the grades
   double avg; // avg of the grades (change to mean later)
   final int SIZE = 23; // size of the array
   int[] scores = new int[SIZE]; // array to store the scores
   int i = 0; // array subscript
   int high = 0; // variable to hold the high grade
   int low = 0; // variable to hold the low grade


  // Read the input and write the output

  out.printf("The scores are: ");

  while (in.hasNextInt())
  {
     // statements to input the scores, output the scores, accumulate the 
     sum, and modify the subscript
     if (i >= 0 && i <= 100)
     {
       scores[i] = in.nextInt();
       sum = sum + scores[i];
       out.print(scores[i]);
       i++;
     }
     else
     {
       System.out.println("The ignored scores are: " + scores[i]);
     }  

  }

您似乎将索引与输入的值混淆了

将您的代码更改为

while (in.hasNextInt())
{
 // statements to input the scores, output the scores, accumulate the 
 int input = in.nextInt();
 if (input >= 0 && input <= 100)
 {
   scores[i] = input;
   sum = sum + scores[i];
   out.print(scores[i]);
   i++;
 }
 else
 {
   System.out.println("The ignored score is: " + input);
 }  

}

代码应该检查分数,而不是数组索引 i

while (in.hasNextInt())
{
 // statements to input the scores, output the scores, accumulate the 
 sum, and modify the subscript
 int score = in.nextInt()
 if (score >= 0 && score <= 100)
 {
   scores[i] = score;
   sum = sum + scores[i];
   out.print(scores[i]);
   i++;
 }
 else
 {
   System.out.println("The ignored scores are: " + score);
 }  
}