簡體   English   中英

Java - 最高,最低和最低

[英]Java - Highest, Lowest and Average

我剛開始學習,我需要一些練習幫助。

我需要最終用戶輸入每個月的降雨量。 然后我需要把平均降雨量,最高月份和最低月份以及降雨量高於平均值的月份排除在外。

我一直在最高和最低的數字,我不知道為什么。 我正在認真地拔掉頭發。 任何幫助將不勝感激。

這是我到目前為止:

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;
    }

}

謝謝,

艾米莉

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

現在讓我們來看看你的代碼

  1. lowest = numgroup[0];

為什么? 您似乎嘗試使用此算法來查找min:

1.1假設第一個數字(你認為是numgroup[0] )是min(在你的代碼中命名為lowest
1.2。 將其與所有其他數字進行比較,如果任何數字較小,則替換min(即lowest )。

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

for(index = 1;...

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

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

for(minIndex = 0;

而索引0處的元素甚至不打算供您使用(我猜)

接下來,您找出當前迭代中的數字是否小於lowest

if (numgroup[0] < lowest)

總是將索引0處的元素與lowest元素進行比較(我猜)不是你的意圖。

代替

if (numgroup[0] < lowest)

你必須寫

if (numgroup[minIndex] < lowest)

同樣的道理

if (numgroup[1] > highest)

應該是

if (numgroup[maxIndex] > highest)

你做

lowest = numgroup[0]

接着

if (numgroup[0] < lowest)

因為numgroup [0]總是等於最低,所以永遠不會是“真”。 而你的if子句應該是if (numgroup[minIndex] < lowest) 同樣的事情適用於最高。

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");

}

如果您的作業沒有限制,只需嘗試使用最小/最大的集合。

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;
}
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];
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM