为什么我的程序没有返回正确的数字?

时间:2015-03-08 03:42:03

标签: java arrays methods

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;
    }

}

大家好!现在我的代码存在问题,我无法弄清楚原因。该代码应该发回高于或等于平均值​​的分数和低于平均值的分数。

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

6 个答案:

答案 0 :(得分:2)

for (int index = 1; index < score.length; index++)

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

答案 1 :(得分:2)

其他人回答说问题在于for循环中的索引应该是基于0的,这肯定是代码中的错误。但是,这问题。

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

private static int scoreAverage;

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

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

来修复代码
 private static double scoreAverage;

它必须是double,而不是int,因为除法返回浮点类型,而不是整数。

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

正在检查0getHeighest()getLowest()索引中的元素。因此,您可以在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;
    }

}

答案 4 :(得分:0)

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

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

但是您的代码还存在另一个问题,即您声明了两个名为scoreAverage的变量:一个在主方法中:
double scoreAverage;
而另一个时间稍微低于主要方法作为静态字段:
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; ...

您的代码应该有效。

答案 5 :(得分:0)

您正在调用的方法: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;
    }