-1

我的程序输出有问题。它应该找到总降雨量、平均降雨量和降雨量最少/最多的月份。该程序运行良好,但是当我运行它时,它总是显示一月为最低(当它不是)而十二月是最高(当它不是)。我希望找出我做错了什么。

public class RainFall {
double[] rainArray = new double [SIZE]; 
public static final int SIZE = 12;



RainFall() {     
}
public RainFall(double[] r){
    rainArray = new double[SIZE];

}

public void setArrayValueAtIndex( double Val, int Index) {
    if (Index < SIZE - 1)
        return;

    rainArray[Index] = Val;
}

public double getTotal() {
    double total = 0;
    for (int i = 0; i < SIZE; i++){
        total += rainArray[i];
    }

    return total;
}

public String getMonth ( int monthIndex) {
    switch( monthIndex )
    {
        case 0 : return "January";
        case 1 : return "February";
        case 2 : return "March";
        case 3 : return "April";
        case 4 : return "May";
        case 5 : return "June";
        case 6 : return "July";
        case 7 : return "August";
        case 8 : return "Septemeber";
        case 9 : return "October";
        case 10 : return "November";
        case 11 : return "December";
        default : return "Error";
    }

}


public double getAverage(){
    return (getTotal() / SIZE);
}

public String getMost(){
    int mostRain = 0;
    for (int i = 1; i < SIZE; i++)
    {
       if (rainArray[i] > rainArray[mostRain])
           mostRain = i;
    }
    return getMonth( mostRain );

}

public double getMostValue(){
    int mostRain = 0;
    for ( int i = 1; i  < SIZE; i++) {
        if (rainArray[i] < rainArray[mostRain])
        mostRain = i; 
    }
    return rainArray[mostRain];

}

public String getLeast(){
    int leastRain = 0;
    for ( int i = 1; i < SIZE; i++ ) {
        if ( rainArray[i] < rainArray[leastRain])
            leastRain = i;
    }

    return getMonth( leastRain );

}

public double getLeastValue(){
    int leastRain = 0;
    for ( int i = 1; i < SIZE; i++) {
        if (rainArray[i] < rainArray[leastRain])
         leastRain =  i;
    }

    return rainArray[leastRain];
}
        }

这个程序有两个部分,但我确信问题出在这部分。

4

2 回答 2

3
public double getMostValue(){
    int mostRain = 0;
    for ( int i = 1; i  < SIZE; i++) {
        if (rainArray[i] < rainArray[mostRain])
        mostRain = i; 
    }
    return rainArray[mostRain];
}

这将找到最小值。您需要<>.

于 2013-05-10T09:22:55.170 回答
0

我不明白,该方法是找到最小值但命名为最多..

public double getMostValue(){
    int mostRain = 0;
    for ( int i = 1; i  < SIZE; i++) {
        if (rainArray[i] < rainArray[mostRain])
        mostRain = i; 
    }
    return rainArray[mostRain];

}
于 2013-05-10T10:06:11.450 回答