当前位置: 首页 > 知识库问答 >
问题:

试图用最高值和最低值获取要显示的正确月份

洪捷
2023-03-14

我能够得到正确的最高和最低值,但它不是把正确的月份与该值。下面是降雨类和降雨程序。下面是我放入的值(2.1、1.7、3.5、2.6、3.7、1.6、3.9、2.6、2.9、4.3、2.4、3.7)。我会在最后展示结果。

/**
 * RainFall class 
 */

public class RainFall 
{
    private double[] rainValue; //Rain entered by user
    private int months = 12; //Number of months in a year
    /**
    * Constructor
    */

    public RainFall(double[] rainArray)
    {
        rainValue = rainArray;
    }
    /**
     * The getRainSum method returns the sum of all 
     * rainfall for the year. 
     */
    public double getRainSum() 
    {
        double sum = 0.0; //Accumulator

        //Get sum of all values in the rain array.
        for (double value : rainValue)
            sum += value;

        return sum;


    }

    /**
     *The get RainAverage method returns the monthly  
     *rainfall average. 
     */
    public double getRainAverage()
    {
        return getRainSum() / months;
    }

    /**
     * The getRainHighest method returns the highest
     * rainfall month for the year.
     */
    public double getRainHighest()
    {
        double highest = rainValue[0]; //Get value at index 0

        //Search array for the highest value.
        for(int index = 1; index < rainValue.length; index++)
        {
            if (rainValue[index] > highest)
                highest = rainValue[index];
        }
        return highest; // return highest value
    }

    /**
     * The getRainLowest method returns the lowest
     * rainfall month for the year
     */
    public double getRainLowest()
    {
        double lowest = rainValue[0]; //Get value at index 0

        //Search array for the lowest value.
        for (int index = 1; index < rainValue.length; index++)
        {
            if (rainValue[index] < lowest)
                lowest = rainValue[index];
        }
    return lowest; //return lowest value
    }

}
/**
 * This program demonstrates the RainFall class.
 */
public class RainTest {

    public static void main(String[] args) 
    {

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

        //Crate an array to hold the rain values
        double[] rain = new double[12];

        //Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);
        //Get rain values and store them 
        //in the rain array
        for (int index = 0; index < months.length; index++)
        {           
            System.out.print("Enter rain value for " + months[(index)] + ":");
            rain[index] = keyboard.nextDouble();
            if (rain[index] <0)
            {
                System.out.println("You can not use a negative number.\n");
                index--;
            }

        }

        /**
         * Create a RainFall object, passing the rain array 
         * as an argument to the constructor.
         */
        RainFall myRainFall = new RainFall(rain);

        //Display total rainfall
        System.out.println("The total rainfall for the year is: " + myRainFall.getRainSum());

        //Display average rainfall 
        System.out.println("The average rainfall for the year is: " + myRainFall.getRainAverage());

        //Display highest rainfall

        System.out.println("The month with the highest rainfall was " + months[(int) myRainFall.getRainHighest() -1] +" with " 
                            + myRainFall.getRainHighest() +" inches.");

        //Display lowest rainfall

        System.out.println("The month with the lowest rainfall was "  + months[(int) myRainFall.getRainLowest() -1] + " with " 
                           + myRainFall.getRainLowest() +" inches.");

    }

}
Enter rain value for January:2.1
Enter rain value for February:1.7
Enter rain value for March:3.5
Enter rain value for April:2.6
Enter rain value for May:3.7
Enter rain value for June:1.6
Enter rain value for July:3.9
Enter rain value for August:2.6
Enter rain value for September:2.9
Enter rain value for October:4.3
Enter rain value for November:2.4
Enter rain value for December:3.7
The total rainfall for the year is: 35.0
The average rainfall for the year is: 2.9166666666666665
The month with the highest rainfall was April with 4.3 inches.
The month with the lowest rainfall was January with 1.6 inches.

提前感谢您所能提供的任何帮助。

共有1个答案

松英喆
2023-03-14

您的问题是getRainLowsow返回最低月份的降雨量,而不是最低月份的索引。因此,以下表达式没有意义:

months[(int) myRainFall.getRainLowest() - 1]

您基本上需要定义一个不同的方法,它给出最低月份的指数,而不是最低月份的降雨量。最高月份的持有量相同。

 类似资料:
  • 请帮助我,我已经有一个代码,它运行完美,但问题是我需要确定每行的最高和最低。我不知道如何开始,请帮助我,也请向我解释。这是代码:

  • 问题内容: 我尝试在执行以下命令后获得具有最高/最低编号的行: 这是我的测试数据 为了获得最低的价值,我将使用 现在,id是,但应该是。 我也尝试了加入: 如何根据最低的结果为每个结果获取正确的ID ? 问题答案: 我认为这是您要实现的目标: 输出: ID 价值 姓名 1个 10 第1行 4 5 第2行 在这里,我使用minVal和Name自联接了表。

  • 我能够输出最高的值,但我似乎不能让它工作时,我这样做。getnames,因为它显示我一个错误 错误是我似乎不能显示关联名称的最高值它只显示余额

  • 问题内容: 我当前正在编写一个程序,其中用户必须输入10个数字,然后输出将是最高数字和最低数字。我的代码有问题,但找不到。 问题答案: 以不同的方式初始化值: 如果将它们都初始化为零,则永远不会有小于零的“最高”值或大于零的“最低”值

  • 我正在写一个方法,它接受用户的整数输入,并显示总数、平均值、最大值和最小值。 我有总数和平均工作,但我得到2147483647最大和-2147483648最小。 循环只能在用户输入-1时结束。 我的代码:

  • 我的数据库结构包含列:。我想为每个检索具有最低的行。我一直试图把和搞乱,仍然没有解决方案。