2016-11-15 2 views
0

올바른 최고/최저 값을 얻을 수 있지만 그 값으로 정확한 월을 넣지는 않습니다. 아래는 RainFall Class와 Rainfall 프로그램입니다. 다음은 내가 입력 한 값입니다 (2.1, 1.7, 3.5, 2.6, 3.7, 1.6, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7). 결국 결과를 보여 드리겠습니다. 당신이 가장 높은 달 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. 

:

/** 
* 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 
    } 

} 

테스트 프로그램 : 여기

/** 
* 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

전체 강우량을 호출하고 하나씩 빼서 어느 월이 가장 낮거나 높습니까? 따라서 정수로 4.3 - 1은 4 월에 3 월이되고 4 번째 달에 0 1 2 3으로 계산됩니다.

0부터 시작하여 더 낮은 값을 볼 때 다른 변수를 색인으로 설정합니다.

0

감사합니다. 다시 돌아가 최상위 및 최저 인덱스를 찾기 위해 코드를 변경 한 다음 get 문을 추가하여 해당 인덱스 값을 반환했습니다.

관련 문제