【问题标题】:Why doesn't my program return the correct number?为什么我的程序没有返回正确的数字?
【发布时间】:2015-05-09 11:26:25
【问题描述】:
import java.util.Scanner;

public class AnalyzingScores
{
  public static void main(String[] args)
  {
    int count = 0;
    double scoreTotal = 0;
    int index;
    int tests;
    double scoreAverage;
    double highest;
    double lowest;

    //Creates a new Scanner.
    Scanner keyboard = new Scanner(System.in);

    //Asks the user to enter how many tests they have taken.
    System.out.println("Enter the amount of tests you have taken: ");
    tests = keyboard.nextInt();

    //Creates an array.
    int[] score = new int[tests];

    //Creates a for loop that asks a user to enter their test scores.
    for (index = 0; index < tests; index++)
    {
      System.out.println("Enter your test score: ");
      score[index] = keyboard.nextInt();

      scoreTotal += score[index];
    }
    scoreAverage = scoreTotal / score.length;

    System.out.println("You entered " + tests + " scores.");
    System.out.println("The test average is " + scoreAverage);
    System.out.println("Number of scores above or equal to the average is " + getHighest(score));
    System.out.println("Number of scores below the average is " + getLowest(score));
    }

    private static int scoreAverage;

    public static int getHighest(int[] score)
    {
      int aboveAverage = 0;

      for (int index = 1; index < score.length; index++)
      {
        if (score[index] >= scoreAverage)
        {
          aboveAverage++;
        }
      }

      return aboveAverage;
    }

    public static int getLowest(int[] score)
    {
      int belowAverage = 0;

      for (int index = 1; index < score.length; index++)
      {
        if (score[index] < scoreAverage)
        {
          belowAverage++;
        }
      }

      return belowAverage;
    }

}

大家好!现在我的代码有问题,我不知道为什么。该代码应该发送回高于或等于平均数和低于平均数的分数。

但有时,代码不会返回正确的数字。例如,如果我输入 100、90 和 80 的 3 个测试分数,则平均值为 90。它应该显示 2 高于/等于 90 和 1 低于 90。问题是,它显示 0低于 90。我已经对其进行了多次测试,但似乎只发生在低于平均水平。提前致谢!

【问题讨论】:

  • 我认为那是因为您以index=1 开头以index=0 开头

标签: java arrays methods


【解决方案1】:

其他人回答说问题出在你的 for 循环中的索引应该是从 0 开始的,这肯定是你代码中的一个错误。然而,这不是问题。

在函数getHighest()getLowest() 中,变量scoreAverage 指的是在函数定义之前声明的未初始化的类级变量,即:

private static int scoreAverage;

不是在函数main() 中声明的scoreAverage。类级别变量默认为 0,这就是为什么您看不到低于平均值的值。

您可以通过删除main() 中的scoreAverage 声明并将类级别声明修改为:

 private static double scoreAverage;

它必须是双精度,而不是整数,因为除法返回的是浮点类型,而不是整数。

或者,您可以使用在 main() 中声明的 double 类型的变量作为平均得分,并将其传递给两个函数,而不是访问类级别的变量,这可能更可取。

【讨论】:

  • 非常感谢!为我修好了。
【解决方案2】:
for (int index = 1; index < score.length; index++)

Java(和大多数编程语言)中的数组使用0-based numbering。您的循环应该从int index = 0 开始,而不是int index = 1

【讨论】:

    【解决方案3】:

    getHighest(int[] score)getLowest(int[] score) 方法中,数组的索引应该从0 开始,而不是1。这样您就错过了输入的第一个值。

    【讨论】:

    • 我会赞成这个答案,但它缺少一些帮助他的代码。
    【解决方案4】:

    没有检查getHeighest()getLowest()0th 索引中的元素。因此,您在aboveAveragebelowAverage 中获得的值比您预期的值少一个

    删除该行

    private static int scoreAverage;
    

    并将scoreAverage 传递给这两个函数。

    代码:

    public static int getHighest(int[] score, double scoreAverage)
        {
          int aboveAverage = 0;
          //Index should start from 0 to avoid skipping the first element of the array.
          for (int index = 0; index < score.length; index++)
          {
            if (score[index] >= scoreAverage)
            {
              aboveAverage++;
            }
          }
    
          return aboveAverage;
        }
    
        public static int getLowest(int[] score, double scoreAverage)
        {
          int belowAverage = 0;
          //Here also, index should start from 0.
          for (int index = 0; index < score.length; index++)
          {
            if (score[index] < scoreAverage)
            {
              belowAverage++;
            }
          }
    
          return belowAverage;
        }
    
    }
    

    【讨论】:

    • 感谢您和其他所有提供帮助的人,但代码现在显示有 3 个高于/等于平均值​​。
    【解决方案5】:

    正如其他人提到的,你的 for 循环应该从索引 0 开始:

    for (int index = 0; index < scrore.length; index++)
    

    但是您的代码还有另一个问题是您声明了两个名为 scoreAverage 的变量:一个在 main 方法中:
    double scoreAverage;
    还有一次在 main 方法下面一点作为静态字段:
    private static int scoreAverage;
    因此,您只需进行一项更改:

    private static double scoreAverage;
    
    public static void main(String[] args)
    {
    int count = 0;
    double scoreTotal = 0;
    int index;
    int tests;
    //double scoreAverage; <--- remove that
    double highest;
    double lowest; ...
    

    你的代码应该可以工作。

    【讨论】:

      【解决方案6】:

      您调用的方法:getHighest() 和 getLowest() 没有正确的 scoreAverage 变量。在该方法的代码中, scoreAverage 变量等于 0。所以你可以这样做:

      public static int getHighest(int[] score, double scoreAverage)
          {
            int aboveAverage = 0;
      
            for (int index = 0; index < score.length; index++)
            {
              if (score[index] >= scoreAverage)
              {
                aboveAverage++;
              }
            }
      
            return aboveAverage;
          }
      
          public static int getLowest(int[] score, double scoreAverage)
          {
            int belowAverage = 0;
      
            for (int index = 0; index < score.length; index++)
            {
              if (score[index] < scoreAverage)
              {
                belowAverage++;
              }
            }
      
            return belowAverage;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-13
        • 2015-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多