2010-05-17 183 views
1

我剛剛開始學習,我需要幫助完成一項練習。Java - 最高,最低和平均水平

我需要最終用戶輸入每個月的降雨編號。 然後我需要輸出平均降雨量,最高月份和最低月份以及降雨量高於平均水平的月份。

我不斷收到最高和最低的相同數字,我不知道爲什麼。我正在認真地拉我的頭髮。任何幫助將不勝感激。

這是我到目前爲止有:

public class rainfall { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
    int[] numgroup; 
    numgroup = new int [13]; 
    ConsoleReader console = new ConsoleReader(); 
    int highest; 
    int lowest; 
    int index; 
    int tempVal; 
    int minMonth; 
    int minIndex; 
    int maxMonth; 
    int maxIndex; 


    System.out.println("Welcome to Rainfall"); 

    for(index = 1; index < 13; index = index + 1) 
    {  
     System.out.println("Please enter the rainfall for month " + index); 
       tempVal = console.readInt(); 
       while (tempVal>100 || tempVal<0) 
        { 
        System.out.println("The rating must be within 0...100. Try again"); 
        tempVal = console.readInt(); 
        } 
       numgroup[index] = tempVal; 
    }   



    lowest = numgroup[0]; 


     for(minIndex = 0; minIndex < numgroup.length; minIndex = minIndex + 1); 
     { 
       if (numgroup[0] < lowest) 
       { 
       lowest = numgroup[0]; 
       minMonth = minIndex; 
       } 
     } 

    highest = numgroup[1]; 


      for(maxIndex = 0; maxIndex < numgroup.length; maxIndex = maxIndex + 1); 
      { 
        if (numgroup[1] > highest) 
        { 
        highest = numgroup[1]; 
        maxMonth = maxIndex; 
        } 
      } 


     System.out.println("The average monthly rainfall was "); 
     System.out.println("The lowest monthly rainfall was month " + minIndex); 
     System.out.println("The highest monthly rainfall was month " + maxIndex); 

     System.out.println("Thank you for using Rainfall"); 

    } 


    private static ConsoleReader ConsoleReader() { 

     return null; 
    } 

} 

感謝,

艾米莉

+2

Java中的數組基於零。你必須最終習慣它。 – 2010-05-17 09:51:51

回答

1

,而不是

if (numgroup[0] < lowest) 

你必須寫

if (numgroup[minIndex] < lowest) 

也是如此

if (numgroup[1] > highest) 

這應該是

if (numgroup[maxIndex] > highest) 
+0

謝謝,所以我換了這些,但我不斷收到此錯誤: 異常線程「main」 java.lang.ArrayIndexOutOfBoundsException:13 \t在rainfall.main(rainfall.java:43) – Emily 2010-05-17 09:54:59

1

你做

lowest = numgroup[0]

然後

if (numgroup[0] < lowest)

由於numgroup [0]始終等於最低值,所以它永遠不會爲「真」。相反,你的if子句應該是if (numgroup[minIndex] < lowest)。同樣的事情適用於最高。

0
public static void main(String[] args) 
{ 
int[] numgroup = new int [12]; // 12 months - 12 elements 
ConsoleReader console = new ConsoleReader(); 
int highest; 
int lowest; 
int index; 
int tempVal; 
int minIndex; 
int maxIndex; 


System.out.println("Welcome to Rainfall"); 
// Input (index now 0-based) 
for(index = 0; index < 12; index = index + 1) 
{  
    System.out.println("Please enter the rainfall for month " + index + 1); 
    tempVal = console.readInt(); 
    while (tempVal>100 || tempVal<0) 
    { 
     System.out.println("The rating must be within 0...100. Try again"); 
     tempVal = console.readInt(); 
    } 
    numgroup[index] = tempVal; 
}   

lowest = numgroup[0]; 
highest = numgroup[0]; 
int total = 0.0; 
// Loop over data (using 1 loop) 
for(index = 0; index < 12; index = index + 1) 
{  
    int curr = numgroup[index]; 
    if (curr < lowest) { 
     lowest = curr; 
     minIndex = index; 
    } 
    if (curr > highest) { 
     highest = curr; 
     maxIndex = index; 
    } 
    total += curr; 
} 
float avg = (float)total/numgroup.length; 

System.out.println("The average monthly rainfall was " + agv); 
// +1 to go from 0-based index to 1-based month 
System.out.println("The lowest monthly rainfall was month " + minIndex + 1); 
System.out.println("The highest monthly rainfall was month " + maxIndex + 1); 

System.out.println("Thank you for using Rainfall"); 

} 
+0

所以,我想這一點,它的未來與:螺紋 異常「主要」 java.lang.Error的:未解決的編譯問題: \t局部變量minIndex可能沒有被初始化 \t局部變量maxIndex可能沒有被初始化 \t的降雨。主要(rainfall.java:59) – Emily 2010-05-17 10:07:51

0

只是嘗試使用集合的最小/最大,如果沒有限制你的功課。

Vector<Integer> rainfallData = new Vector<Integer>(); 
int avg = 0; 

for(index = 1; index < 13; index = index + 1) 
{  
    System.out.println("Please enter the rainfall for month " + index); 
    tempVal = console.readInt(); 
    while (tempVal>100 || tempVal<0) 
    { 
     System.out.println("The rating must be within 0...100. Try again"); 
     tempVal = console.readInt(); 
    } 
    rainfallData.add(tempVal); 
    avg+=tempVal; 
} 

avg /= rainfallData.size(); 
int min = Collections.min(rainfallData); 
int max = Collections.max(rainfallData); 

否則,最小/最大應該是這樣的:

public int min(int[] vals) { 
    if(vals==null || vals.length==0} { 
      throw new IllegalArgumentException(); 
    } else if(vals.length == 1) { 
     return vals[0]; 
    } 
    int min = vals[0]; // Dont initialize with Integer.MAX_VALUE or so 
    for(int i = 1; i < vals.length; ++i) { 
     if(vals[i] < min) { 
      min = vals[i]; 
     } 
    } 
    return min; 
} 
0
for (index = 0; index < 12; index++) { 

} 

變化的第一個for循環及以下

lowest = numgroup[0]; 

for (minIndex = 0; minIndex < numgroup.length; minIndex = minIndex + 1) 
{ 
    if (numgroup[minIndex] < lowest) { 
     lowest = numgroup[minIndex]; 
    } 
} 

highest = numgroup[0]; 

for (maxIndex = 0; maxIndex < numgroup.length; maxIndex = maxIndex + 1) 
{ 
    if (numgroup[maxIndex] > highest) { 
     highest = numgroup[maxIndex]; 
    } 
} 
+0

不是,這不起作用,它輸出: 月最低的降雨量是12月 月最高降雨量爲12個月 – Emily 2010-05-17 10:12:26

2

首先,因爲這是你的功課,你不應該問它在stackoverflow.com

現在讓我們看看你的代碼

  1. lowest = numgroup[0];

爲什麼?看來你嘗試使用這個算法來尋找分鐘:

1.1 Suppose first number (which you think is numgroup[0]) is min (named as lowest in your code)
1.2. Compare it with all other numbers, if any of the numbers is smaller, replace min (i.e lowest).

但是,numgroup[0]是不是你的第一個數字!你開始你的第一個for循環這樣

for(index = 1;...

所以,你的第一個數字是numgroup[1]

接下來,你的第二個循環開始像

for(minIndex = 0;

,而該元素在索引0從來沒有打算由你(我猜)使用

接下來,你的如果查不到條件在當前迭代次數小於lowest

if (numgroup[0] < lowest)

它總是在索引0處與lowest比較元件,其(I猜)不是ÿ我們的意圖。

+0

這就是我所說的,但由於這是更好的格式,所以+1,我會刪除我的答案。 – 2010-05-17 16:02:34