如何重做两个简单的Java方法

时间:2018-10-20 16:55:31

标签: java arrays for-loop methods constructor

我正在编写一个程序,该程序计算一年的总降雨量,并返回该年的总降雨量,平均降雨量和带有最高和最低降雨量。我的代码可以正确编译,但是在运行主类后,我的两个方法类getMostRain()和getLeastRain()似乎已损坏,而我对发生这种情况的原因感到困惑。有人可以把我引向我做错了吗?

Rainfall.java方法类

public class Rainfall
{
  private double[] rainfallStats;
  public Rainfall(double[] arrayGiven)
  {
    rainfallStats = new double[arrayGiven.length];
    for(int i = 0; i < arrayGiven.length; i++)
    {
      rainfallStats[i] = arrayGiven[i];
    }
  }

  public double getTotalRain()
  {
    double totalRainfall = 0;
    for(int i = 0; i < rainfallStats.length; i++)
    {
      totalRainfall += rainfallStats[i];
    }
    return totalRainfall;
  }

  public double getAverageRain()
  {
    return (getTotalRain()/rainfallStats.length);
  }

  public double getMostRain()
  {
    double mostRain = rainfallStats[0];
    for(int i = 0; i < rainfallStats.length; i++)
    {
      if(rainfallStats[0] > mostRain)
      {
        mostRain = rainfallStats[0];
      }
    }
    return mostRain;
  }

  public double getLeastRain()
  {
    double leastRain = rainfallStats[0];
    for(int i = 0; i < rainfallStats.length; i++)
    {
      if(rainfallStats[0] < leastRain)
      {
        leastRain = rainfallStats[0];
      }
    }
    return leastRain;
  }
}

RainfallTest.java主类

import java.util.Scanner;

public class RainfallTest
{
  public static void main(String[] args)
  {
    Scanner keyboard = new Scanner(System.in);
    final int MONTHS = 12;
    double[] rainfallStats = new double[MONTHS];

    for(int i = 0; i < rainfallStats.length; i++)
    {
      System.out.println("Enter rainfall figure for month: " + (i + 1) + ": ");
      rainfallStats[i] = keyboard.nextDouble();
    }
    Rainfall rainfall = new Rainfall(rainfallStats);
    System.out.println("Total rainfall: " + rainfall.getTotalRain());
    System.out.println("Average rainfall: " + rainfall.getAverageRain());
    System.out.println("Lowest rain month: " + rainfall.getLeastRain());
    System.out.println("Highest rain month: " + rainfall.getMostRain());
  }
}

代码输出:

roosevelt@R-Linux:~/Java/COP2800/CH.7/Challenges$ java RainfallTest 
Enter rainfall figure for month: 1: 
2
Enter rainfall figure for month: 2: 
2
Enter rainfall figure for month: 3: 
2
Enter rainfall figure for month: 4: 
2
Enter rainfall figure for month: 5: 
100
Enter rainfall figure for month: 6: 
2
Enter rainfall figure for month: 7: 
2
Enter rainfall figure for month: 8: 
2
Enter rainfall figure for month: 9: 
0
Enter rainfall figure for month: 10: 
2
Enter rainfall figure for month: 11: 
2
Enter rainfall figure for month: 12: 
2
Total rainfall: 120.0
Average rainfall: 10.0
Lowest rain month: 2.0
Highest rain month: 2.0

2 个答案:

答案 0 :(得分:0)

您的代码返回的数量而不是最多和最少雨量的索引。
更改为:

public int getMostRain()
{
    double mostRain = rainfallStats[0];
    int index = 0;
    for(int i = 1; i < rainfallStats.length; i++)
    {
        if(rainfallStats[i] > mostRain)
        {
            mostRain = rainfallStats[i];
            index = i;
        }
    }
    return index;
}

如果要返回降雨最多而不是降雨量最多的月份。
请记住,索引是从零开始的,因此在打印返回值时,使用必须将+1添加到返回值中。
另外要少雨:

public int getLeastRain()
{
    double leastRain = rainfallStats[0];
    int index = 0;
    for(int i = 1; i < rainfallStats.length; i++)
    {
        if(rainfallStats[i] < leastRain)
        {
            leastRain = rainfallStats[i];
            index = i;
        }
    }
    return index;
}

答案 1 :(得分:0)

使用C1 run1 初始化mostRainleastRain变量是可以的;但是,一旦进入循环,您应该改为使用rainfallStats[0],否则将为循环的每次迭代检索数组的第一个元素:

rainfallStats[i]