0

我在一个初学者Java课程中,只是学习数组的概念。我们必须使用注释来表示代码。

我正在尝试制作一个程序,要求用户输入 20 个数字,将它们存储在一个数组中,然后计算并显示:最小数字、最大数字、数字总数和数字的平均值。

通过 jGrasp 运行它时出现大量错误,并且不确定如何修复它们。

有什么建议吗?

public class NumberAnalysisProgram
{
  public static void main (String[] args)
  {


  Scanner input = new Scanner (System.in);

  //A constant for the array size
  final int SIZE = 20;

  //Declare an array to hold the numbers entered by the user
  int[] numbers = new int[SIZE];

  //Declare variables to hold the lowest and highest
  int lowest = numbers[0];
  int highest = numbers[0];

  //Declare variable to hold the total
  int sum;

  //Declare a variable to hold the average
  double average;

  //Declare a counting variable to use in the loops
  int index = 0;

  //Explain the program
  System.out.println("This program gets a list of 20 numbers. Then displays:");
  System.out.println(" the lowest number, the highest number, the total of the numbers,     
  and the average.");

  //Get the numbers from the user
  for (int i = 0; i< numbers.length; i++)
  {
  System.out.print("Please enter 20 numbers, each seperated by a space:  ");
  numbers[i] = input.nextInt();
  }


  //Call a method to calculate the lowest and highest numbers
  getLowHigh(numbers);

  //Display the lowest and highest numbers
  System.out.println("The lowest number is:  " + lowest);
  System.out.println("The highest number is:  " + highest);



  //Call a method to calculate the total of the numbers
  sum = getTotal(numbers);

  //Display the sum/total of the numbers
  System.out.println("The total of these numbers are:  " + sum);



  //Call a method to calculate the average of the numbers
  average = getAverage(sum, numbers);

  //Display the average of the numbers
  System.out.println("The average of these numbers are:  " + average);

 }


  //Method getLowHigh
  public static int getLowest(int[] array)
  {
     for(int i=1; i< numbers.length; i++)
     {
          if(numbers[i] > highest)
          highest = numbers[i];
       else if (numbers[i] < lowest)
       lowest = numbers [i];
     }

     return highest;
     return lowest;   
  }

  //Method getTotal
  public static int getTotal(int[] array)
  {

  //Loop counter variable
  int index;

  //Accumulator variable initialized to 0
  int total = 0;

  //Pull in the numbers from the main method to calculate the total
  for(index = 0; index < numbers.length; index++)
  {
     total = total + number[index];
  }

     return total;  
  }

  //Method getAverage
  public static int getAverage(int[] array)
  {

     //Loop counter variable
     int index;


     //Pull in the sum and the numbers from the main method to calculate the average
     for(index = 0; index < numbers.length; index++)
     {
        average = sum / number[index];
     }

     return average;  
   }
}
4

3 回答 3

1

我可以看到的第一个问题是,在所有方法中,您从未使用过参数。您使用了不同的数组,这些方法中不存在该数组。

第二个问题是您试图从一种方法返回两个值。一个方法只能返回一个值。因此,您必须摆脱“ return highest;”并复制没有“ return lowest;”的方法,并在需要的地方使用每个方法。

我看到的第三件事(尽管它没有引起任何错误)是你可以通过说来缩短代码

int total = 0;

for(int index = 0; index < numbers.length; index++)
{
   total += number[index];
}

代替

int index;

int total = 0;

for(index = 0; index < numbers.length; index++)
{
   total = total + number[index];
}
于 2013-10-17T02:37:28.690 回答
0

对于初学者,下面的代码试图将变量初始化为不存在的数组值……这些应该完全删除。

//Declare variables to hold the lowest and highest
  int lowest = numbers[0];
  int highest = numbers[0];

下面的代码中也会出现错误,因为您没有将它们传递给函数,因此它不存在于此范围内。public static int getLowest(int[] array) { for(int i=1; i< numbers.length; i++) { if(numbers[i] > high) highest = numbers[i]; 否则如果(数字[i] <最低)最低=数字[i];}

     return highest;
     return lowest;   
  }

您还为只需要一个的函数提供 2 个参数。见下文:

//Call a method to calculate the average of the numbers
  average = getAverage(sum, numbers);

public static int getLowest(int[] array)
  {
     for(int i=1; i< numbers.length; i++)
     {
          if(numbers[i] > highest)
          highest = numbers[i];
       else if (numbers[i] < lowest)
       lowest = numbers [i];
     }

     return highest;
     return lowest;   
  }
于 2013-10-17T02:26:50.727 回答
0

我认为这更干净:

public class Main {
    public static final int SIZE = 20;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        List<Integer> numbers = new ArrayList<Integer>();

        Double total = 0d;

        System.out.println("This program gets a list of 20 numbers. Then displays:");
        System.out.println(" the lowest number, the highest number, the total of the numbers, and the average.");

        // Get the numbers from the user
        System.out.print("Please enter 20 numbers, each seperated by a space:  ");
        for (int i = 0; i < SIZE; i++) {
            Integer currInput = input.nextInt();
            numbers.add(currInput);
            total += currInput;
        }

        System.out.println("The lowest number is:  " + Collections.min(numbers));
        System.out.println("The highest number is:  " + Collections.max(numbers));
        System.out.println("The total of these numbers are:  " + total);
        System.out.println("The average of these numbers are:  " + (total / SIZE));

    }
}

希望能帮助到你 :]

于 2013-10-17T02:49:25.353 回答