0

立ち往生しています...私のコードは以前は機能していましたが、今はハングします。getHighestその上、メソッドとgetSmallestメソッドが正しい値を返すことができないようです。何かをキャッチしていないだけかどうかはわかりません。どんな助けでも素晴らしいでしょう!

import java.util.Scanner;
import java.text.DecimalFormat;

public class Rainfall
{
  public static void main(String[] args)
  {
    final int MONTHS = 12;
    double[] rain = new double[MONTHS];
    initRain(rain);
    double total = totalRain(rain);
    double average = averageRain(rain, total);
    double most = mostRain(rain);
    double least = leastRain(rain);
    DecimalFormat digit = new DecimalFormat("#.0");
    System.out.println("The total rainfall of the year is " + digit.format(total));
    System.out.println("The average rainfall of the year is " + digit.format(average));
    System.out.println("The month with the highest amount of rain is " + most);
    System.out.println("The month with the lowest amount of rain is " + least);
  }
  public static void initRain(double[] array)
  {
    Scanner keyboard = new Scanner(System.in);
    for (int x = 0; x < array.length; x++)
    {  
      System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
      array[x] = keyboard.nextDouble();
    }
  }
  public static double totalRain(double[] array)
  {
    double total = 0;
    for (int x = 0; x < 12; x++)
      total += array[x];
    return total;
  }
  public static double averageRain(double[] array, double total)
  {
    return total / array.length;
  }
  public static double mostRain(double[] array)
  {;
    double maximum = array[1];
    int value = 0;
    for (int i=0; i < 12; i = i++)
    {
      if (array[i] >= maximum)
        maximum = array[i];
        value = i;
      }
    return value;
  }
  public static double leastRain(double[] array)
  {
    double minimum = array[0];
    int value;
    for (int i=0; i < 12; i++)
    {
      if (array[i] <= minimum) 
        minimum = array[i];
        value = i;
      }
    return value;
  }
}
4

1 に答える 1

3

次の行が原因でプログラムがハングします。

for (int i=0; i < 12; i = i++)

問題は、変数がインクリメントされる前のi++の値を返すことです。したがって、ループのインクリメントステップは書き込みと同じです。したがって、変数がループのエスケープ条件に達することはありません。i i=ii

そのはず:

for (int i=0; i < 12; i++)

コードを少しクリーンアップしました。大幅に改善できます。多くの単純なエラーがありました。

import java.util.Scanner;
import java.text.DecimalFormat;

public class Rainfall
{
  public static void main(String[] args)
  {
    final int MONTHS = 12;
    double[] rain = new double[MONTHS];
    initRain(rain);
    double total = totalRain(rain);
    double average = averageRain(rain, total);
    int most = mostRain(rain);
    int least = leastRain(rain);
    DecimalFormat digit = new DecimalFormat("#.0");
    System.out.println("The total rainfall of the year is " + digit.format(total));
    System.out.println("The average rainfall of the year is " + digit.format(average));
    System.out.println("The month with the highest amount of rain is " + (most + 1));
    System.out.println("The month with the lowest amount of rain is " + (least + 1));
  }
  public static void initRain(double[] array)
  {
    Scanner keyboard = new Scanner(System.in);
    for (int x = 0; x < array.length; x++)
    {
      System.out.print("Enter Rainfall for month " + (x + 1) + ": ");
      array[x] = keyboard.nextDouble();
    }
  }
  public static double totalRain(double[] array)
  {
    double total = 0;
    for (int x = 0; x < 12; x++)
      total += array[x];
    return total;
  }
  public static double averageRain(double[] array, double total)
  {
    return total / array.length;
  }
  public static int mostRain(double[] array)
  {
    double maximum = array[1];
    int value = 0;
    for (int i=0; i < 12; i++) {
      if (array[i] >= maximum) {
        maximum = array[i];
        value = i;
      }
    }
    return value;
  }
  public static int leastRain(double[] array)
  {
    double minimum = array[0];
    int value = 0;
    for (int i=0; i < 12; i++)
    {
      if (array[i] <= minimum) {
        minimum = array[i];
        value = i;
      }
    }
    return value;
  }
}
于 2012-12-06T16:54:05.480 に答える