猿问

将数组元素与方法的返回值进行比较

我有一种方法可以根据用户输入的月份数组计算最小和最大降雨量。我的问题是我的方法返回最小值和最大值的数量,我需要它返回降雨量最高或最低的月份。我不知道如何将我的最小和最大方法与月份数组进行比较。我想如果我可以说最小值是在索引 5 处找到的,那么也有办法拉出月份数组的索引 5。


我尝试了多种类型的 for 循环和嵌套 for 循环,但似乎无法获得正确的月份。


public static void main(String[] args) {


    String[] months = {"January", "February", "March","April","May","June","July","August","September","October","November","December"};

    double[] rainfall = new double[months.length];


    Rainfall amazon = new Rainfall();


    //scanner to get user input

    Scanner scanner = new Scanner(System.in);


    //for loop to iterate through the months to get user input of rainfall each month

    for(int i = 0; i<rainfall.length; i++) {

        System.out.println("How much rainfall did you recieve, in inches, for the month of: " + months[i]);

        rainfall[i] = scanner.nextDouble();


        //while statement to reject negative numbers         

        while(rainfall[i] < 0) {

            System.out.println("You can not enter a negative number");

            System.out.println("How much rainfall did you recieve for the month of: " + months[i]);

            rainfall[i] = scanner.nextDouble();  

        }

    }


    System.out.println("total rainfall: " + amazon.getTotalRainfall(rainfall) + 

                       " average Rainfall: " + amazon.getAverageRainfall(rainfall) + 

                       " min: " + amazon.getMinRainfall(rainfall) + 

                       " max: " + amazon.getMaxRainfall(rainfall));


}


public double getMaxRainfall(double[] rainfall) {

    double max = rainfall[0];

    for(int i = 1; i < rainfall.length; i++)

        if(rainfall[i] > max) {

            max = rainfall[i];

        }

    return max;

}      


public double getMinRainfall(double[] rainfall) {

    double min = rainfall[0];

    for(int i = 1; i < rainfall.length; i++)

        if(rainfall[i] < min) {

            min = rainfall[i];

        }

    return min;

}

我需要知道降雨量最高和最低的月份


MMMHUHU
浏览 134回答 4
4回答

眼眸繁星

解决这个问题的方法是跟踪最高降雨量的指数而不是值。您现在遇到的问题是您有 2 个不同的数组,并且其中一个数组的值与另一个数组不相关。为此,你可以这样做:&nbsp; &nbsp; public int getMaxRainfallIndex(double[] rainfall) {&nbsp; &nbsp; &nbsp; &nbsp; int max = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < rainfall.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rainfall[i] > rainfall[max]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return max;&nbsp; &nbsp; }&nbsp;然后,您可以使用此返回值从原始 2 个数组中获取实际降雨量和月份。然而,一种更简洁的方法是创建一个单独的类来存储这两条信息,这样您就可以返回包含这两条信息的整个对象。这样你甚至不必担心保留两个单独的数组。可以这样做:&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Rainfall[] rainfall = new Rainfall[12];&nbsp; &nbsp; &nbsp; &nbsp; //Do everything else&nbsp; &nbsp; }&nbsp; &nbsp; public class Rainfall {&nbsp; &nbsp; &nbsp; &nbsp; private String month;&nbsp; &nbsp; &nbsp; &nbsp; private double rainfall;&nbsp; &nbsp; &nbsp; &nbsp; //Constructor, Getters + Setters&nbsp; &nbsp; }

智慧大石

一个可能的解决方案是返回月份的索引,例如您的getMaxRainfall方法应如下所示:&nbsp;public int getMaxRainfallIndex(double[] rainfall)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int maxIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; double max = rainfall[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 1; i < rainfall.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(rainfall[i] > max)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;max = rainfall[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxIndex = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return maxIndex;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;在您的中,main您应该使用该索引来获取月份和降雨量:int index = amazon.getMaxRainfallIndex(rainfall);system.out.println("month of max rainfall: " + months[index]);system.out.println("max rainfall: " + rainfall[index]);

温温酱

您可以创建一个单独的数组,而不是单独管理两个数组class:public class RainfallByMonth {&nbsp; &nbsp; private double rainfall;&nbsp; &nbsp; private String month;&nbsp; &nbsp; // Getter/Setters here}在你的主要方法中:public static void main(String[] args) {&nbsp; &nbsp; RainfallByMonth[] rainfallByMonths = new RainfallByMonth[12];&nbsp; &nbsp; String[] months = new DateFormatSymbols().getMonths(); // array of months&nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; for (int i = 0; i < rainfallByMonths.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("How much rainfall did you receive, in inches, for the month of: " + months[i]);&nbsp; &nbsp; &nbsp; &nbsp; rainfallByMonths[i].setMonth(months[i]);&nbsp; &nbsp; &nbsp; &nbsp; rainfallByMonths[i].setRainfall(scanner.nextDouble());&nbsp; &nbsp; }&nbsp; &nbsp; RainfallByMonth maxRainfall = getMaxRainfall(rainfallByMonths);&nbsp; &nbsp; // Now you can print the results here}按月计算最大降雨量:public static RainfallByMonth getMaxRainfall(RainfallByMonth[] rainfallByMonths) {&nbsp; &nbsp; RainfallByMonth maxRainfall = rainfallByMonths[0];&nbsp; &nbsp; for(int i = 1; i < rainfallByMonths.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(rainfallByMonths[i].getRainfall() > maxRainfall.getRainfall()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxRainfall = rainfallByMonths[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return maxRainfall;}同样,情况反之亦然min(只是条件不同)。这将更容易管理。

Cats萌萌

public static void main(String[] args) {&nbsp; &nbsp; &nbsp; String[] months = {"January", "February", "March","April","May","June","July","August","September","October","November","December"};&nbsp; &nbsp; &nbsp; int[] weather = {10, -1, 7,44,33,6,22,54,89,21,8,51};&nbsp; &nbsp; &nbsp; int max = 0;&nbsp; &nbsp; &nbsp; int index= 0;&nbsp; &nbsp; &nbsp; for (int i=0;i<weather.length-1;i++&nbsp; ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (weather[i] > m){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = weather[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index= i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; System.out.println(months[index]);&nbsp;}查找最高月份的示例程序。要查找最低月份降雨量,请在if条件中使用小于符号
随时随地看视频慕课网APP

相关分类

Java
我要回答