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

最初の問題は、すべてのメソッドで引数を使用していないことです。これらのメソッド内に存在しない別の配列を使用しました。

2 つ目の問題は、1 つのメソッドから 2 つの値を返そうとしていることです。メソッドから返せる値は 1 つだけです。したがって、" " を取り除き、" return highest;" のないメソッドのコピーを作成し、return lowest;必要な場所でそれぞれを使用する必要があります。

私が見る 3 番目のこと (エラーは発生していませんが) は、次のようにコードを短縮できることです。

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] > maximum) maximum = numbers[i]; それ以外の場合 (数値[i] < 最低) 最低 = 数値 [i]; }

     return highest;
     return lowest;   
  }

また、1 つだけを呼び出す関数に 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 に答える