2016-11-15 4 views
0

正しい最高値と最低値を取得できましたが、その値で正しい月を設定していません。以下はRainFallクラスとRainfallプログラムです。ここに私が入れた値があります(2.1,1.7,3.5,2.6,3.7,1.6,3.9,2.6,2.9,9.3,2.4,3.7)。私は最後に結果を表示します。正しい月を最高値と最低値で表示しようとしています

/** 
* RainFall class 
*/ 

public class RainFall 
{ 
    private double[] rainValue; //Rain entered by user 
    private int months = 12; //Number of months in a year 
    /** 
    * Constructor 
    */ 

    public RainFall(double[] rainArray) 
    { 
     rainValue = rainArray; 
    } 
    /** 
    * The getRainSum method returns the sum of all 
    * rainfall for the year. 
    */ 
    public double getRainSum() 
    { 
     double sum = 0.0; //Accumulator 

     //Get sum of all values in the rain array. 
     for (double value : rainValue) 
      sum += value; 

     return sum; 


    } 

    /** 
    *The get RainAverage method returns the monthly 
    *rainfall average. 
    */ 
    public double getRainAverage() 
    { 
     return getRainSum()/months; 
    } 

    /** 
    * The getRainHighest method returns the highest 
    * rainfall month for the year. 
    */ 
    public double getRainHighest() 
    { 
     double highest = rainValue[0]; //Get value at index 0 

     //Search array for the highest value. 
     for(int index = 1; index < rainValue.length; index++) 
     { 
      if (rainValue[index] > highest) 
       highest = rainValue[index]; 
     } 
     return highest; // return highest value 
    } 

    /** 
    * The getRainLowest method returns the lowest 
    * rainfall month for the year 
    */ 
    public double getRainLowest() 
    { 
     double lowest = rainValue[0]; //Get value at index 0 

     //Search array for the lowest value. 
     for (int index = 1; index < rainValue.length; index++) 
     { 
      if (rainValue[index] < lowest) 
       lowest = rainValue[index]; 
     } 
    return lowest; //return lowest value 
    } 

} 

テストプログラム:あなたは最高の月が10月ではない4月であるべきで、最低は6月ではない1月べきである見ることができるように

Enter rain value for January:2.1 
Enter rain value for February:1.7 
Enter rain value for March:3.5 
Enter rain value for April:2.6 
Enter rain value for May:3.7 
Enter rain value for June:1.6 
Enter rain value for July:3.9 
Enter rain value for August:2.6 
Enter rain value for September:2.9 
Enter rain value for October:4.3 
Enter rain value for November:2.4 
Enter rain value for December:3.7 
The total rainfall for the year is: 35.0 
The average rainfall for the year is: 2.9166666666666665 
The month with the highest rainfall was April with 4.3 inches. 
The month with the lowest rainfall was January with 1.6 inches. 

:ここ

/** 
* This program demonstrates the RainFall class. 
*/ 
public class RainTest { 

    public static void main(String[] args) 
    { 

     String[] months = { "January", "February", "March", 
       "April", "May", "June", "July", 
       "August", "September", "October", 
       "November", "December" }; //Months of the year 

     //Crate an array to hold the rain values 
     double[] rain = new double[12]; 

     //Create a Scanner object for keyboard input. 
     Scanner keyboard = new Scanner(System.in); 
     //Get rain values and store them 
     //in the rain array 
     for (int index = 0; index < months.length; index++) 
     {   
      System.out.print("Enter rain value for " + months[(index)] + ":"); 
      rain[index] = keyboard.nextDouble(); 
      if (rain[index] <0) 
      { 
       System.out.println("You can not use a negative number.\n"); 
       index--; 
      } 

     } 

     /** 
     * Create a RainFall object, passing the rain array 
     * as an argument to the constructor. 
     */ 
     RainFall myRainFall = new RainFall(rain); 

     //Display total rainfall 
     System.out.println("The total rainfall for the year is: " + myRainFall.getRainSum()); 

     //Display average rainfall 
     System.out.println("The average rainfall for the year is: " + myRainFall.getRainAverage()); 

     //Display highest rainfall 

     System.out.println("The month with the highest rainfall was " + months[(int) myRainFall.getRainHighest() -1] +" with " 
          + myRainFall.getRainHighest() +" inches."); 

     //Display lowest rainfall 

     System.out.println("The month with the lowest rainfall was " + months[(int) myRainFall.getRainLowest() -1] + " with " 
          + myRainFall.getRainLowest() +" inches."); 

    } 

} 

結果です。

ご協力いただきありがとうございます。

答えて

2

getRainLowestは、最低月の降雨量を返します。は、で、最も低い月のインデックスです。したがって、次の式では意味をなさない:

months[(int) myRainFall.getRainLowest() - 1] 

あなたは基本的に最低の月のインデックスではなく、最低月の降雨量を与える別の方法を定義する必要があります。最高の月の同じホールド。

0

myRainFall.getRainHighest()は、最も高い降雨値を返します。最も高い降雨値の指標ではありません。あなたは正しい月を印刷している間に最高の雨量の指標を得る必要があります。

+0

"あなたはインデックスを取得する必要があります..." – Vasif

0

合計降雨量を呼び出し、1を減算することによって、どの月が最低/最高かを取得しています。したがって、intとしての4.3 - 1は4月の3月であり、それは4番目の月です。0 1 2 3を返します。

0から始まる別の変数を作成し、その値が低いときはインデックスに設定します。

0

ありがとうございます。私は戻って最高と最低のインデックスを見つけるためにコードを変更し、そのインデックス値を返すためにgetステートメントを追加しました。

関連する問題