-1
我遇到了我的程序輸出問題。它應該找到總降雨量,平均降雨量和雨量最少/最高的月份。該程序運行良好,但是當我運行它時,它總是將1月顯示爲最低(不顯示),而12月顯示最高(不顯示)。我希望找出我做錯了什麼。java程序 - 輸出不是預期的
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];
}
}
有此計劃的兩個部分,但我相信它是這部分的問題是。
感謝您的回覆,我轉向周圍,並將其與幾個月有所幫助。謝謝! – Biowin92 2013-05-10 10:23:04
它解決了你的問題嗎? – christopher 2013-05-10 11:11:47
是的,非常感謝! – Biowin92 2013-05-10 12:05:33