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

时间:2019-10-15 12:00:52

标签: java arrays

我有一种方法可以根据用户输入的月份数组来计算最小和最大降雨量。我的问题是我的方法返回的最小值和最大值的数量,我需要它返回降雨量最高或最低的月份。我不确定如何将我的最小和最大方法与months数组进行比较。我想如果我可以说最小值是在索引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;
}

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

4 个答案:

答案 0 :(得分:2)

解决此问题的方法是跟踪最高降雨量的指数而不是数值。现在的问题是您有2个不同的数组,而一个数组中的值与另一个数组不相关。

为此,您可以执行以下操作:

    public int getMaxRainfallIndex(double[] rainfall) {
        int max = 0;
        for (int i = 1; i < rainfall.length; i++)
            if (rainfall[i] > rainfall[max]) {
                max = i;
            }
        return max;
    } 

然后您可以使用此返回值从原始的2个数组中获取实际的降雨量和月份。

但是,更干净的方法是创建一个单独的类来存储两条信息,以便您可以将包含两条信息的整个对象一起返回。这样,您甚至不必担心保留2个单独的数组。

可以这样做:

    public static void main(String[] args) {
        Rainfall[] rainfall = new Rainfall[12];
        //Do everything else
    }

    public class Rainfall {
        private String month;
        private double rainfall;

        //Constructor, Getters + Setters
    }

答案 1 :(得分:1)

可能的解决方案是返回月份索引,例如,您的getMaxRainfall方法应如下所示:

 public int getMaxRainfallIndex(double[] rainfall)
      {
        int maxIndex = 0;
        double max = rainfall[0];
              for(int i = 1; i < rainfall.length; i++)
                 if(rainfall[i] > max)
                    {
                     max = rainfall[i];
                     maxIndex = i;
                    }
              return maxIndex;
     }   

在您的main中,您应该使用该索引来获取月份和降雨量:

int index = amazon.getMaxRainfallIndex(rainfall);
system.out.println("month of max rainfall: " + months[index]);
system.out.println("max rainfall: " + rainfall[index]);

答案 2 :(得分:0)

您可以创建一个单独的class:而不是分别管理两个数组:

public class RainfallByMonth {
    private double rainfall;
    private String month;

    // Getter/Setters here
}

在您的主要方法中:

public static void main(String[] args) {
    RainfallByMonth[] rainfallByMonths = new RainfallByMonth[12];
    String[] months = new DateFormatSymbols().getMonths(); // array of months

    Scanner scanner = new Scanner(System.in);
    for (int i = 0; i < rainfallByMonths.length; i++) {
        System.out.println("How much rainfall did you receive, in inches, for the month of: " + months[i]);
        rainfallByMonths[i].setMonth(months[i]);
        rainfallByMonths[i].setRainfall(scanner.nextDouble());
    }
    RainfallByMonth maxRainfall = getMaxRainfall(rainfallByMonths);
    // Now you can print the results here
}

按月计算最大降雨量:

public static RainfallByMonth getMaxRainfall(RainfallByMonth[] rainfallByMonths) {
    RainfallByMonth maxRainfall = rainfallByMonths[0];
    for(int i = 1; i < rainfallByMonths.length; i++) {
        if(rainfallByMonths[i].getRainfall() > maxRainfall.getRainfall()) {
            maxRainfall = rainfallByMonths[i];
        }
    }
    return maxRainfall;
}

类似地,min情况反之亦然(只有条件不同)。这将易于管理。

答案 3 :(得分:0)

public static void main(String[] args) {
      String[] months = {"January", "February", "March","April","May","June","July","August","September","October","November","December"};
      int[] weather = {10, -1, 7,44,33,6,22,54,89,21,8,51};
      int max = 0;
      int index= 0;
      for (int i=0;i<weather.length-1;i++  ){
          if (weather[i] > m){
              max = weather[i];
              index= i;
          } 
      }
      System.out.println(months[index]); 
}

查找最高月份的示例程序。 要查找最低月份降雨量,请在 条件下