我正在为我的Java类编写一个程序,要求这样做:

编写一个降雨类,将12个月中的每个月的总降雨量存储为两倍的数组。该程序应具有返回以下内容的方法:


年总降雨量
月平均降雨量
下雨最多的月份
下雨最少的月份


在一个完整的程序中演示该类。 (不接受月降雨量的负数)

import java.util.Scanner;
import java.io.*;
public class apples{
       public static void main (String[] args){
    Scanner kenny = new Scanner(System.in);
    double rain[]=new double[13];
    double sum = 0;
    double avg =0;
    double most =0;
    double least =0;

    System.out.println("Your local weather man here getting paid to tell you the wrong weather!!");
    System.out.println("");
    System.out.println("Please enter in the following rainfall for the months ahead: ");
    System.out.println("Month\tRainfall (In inches)");
    System.out.print("January: ");
    rain [0] = kenny.nextDouble();
    System.out.print("February: ");
    rain [1] = kenny.nextDouble();
    System.out.print("March: ");
    rain [2] = kenny.nextDouble();
    System.out.print("April: ");
    rain [4] = kenny.nextDouble();
    System.out.print("May: ");
    rain [5] = kenny.nextDouble();
    System.out.print("June: ");
    rain [6] = kenny.nextDouble();
    System.out.print("July: ");
    rain [7] = kenny.nextDouble();
    System.out.print("August: ");
    rain [8] = kenny.nextDouble();
    System.out.print("September: ");
    rain [9] = kenny.nextDouble();
    System.out.print("October: ");
    rain [10] = kenny.nextDouble();
    System.out.print("November: ");
    rain [11] = kenny.nextDouble();
    System.out.print("December: ");
    rain [12] = kenny.nextDouble();

    //(Or rain[] = 1,2,3,4,5,6,7,8,9,10,11,12);

    sum = rain[0] + rain[1] + rain[2] + rain[3] + rain[4] + rain[5] + rain[6] + rain[6] + rain[7] + rain[8] + rain[9] + rain[10] + rain[11] + rain[12] ;
    avg = (rain[0] + rain[1] + rain[2] + rain[3] + rain[4] + rain[5] + rain[6] + rain[6] + rain[7] + rain[8] + rain[9] + rain[10] + rain[11] + rain[12]) / 12;

    System.out.println("The sum of all the rain is: " + sum);
    System.out.println("The average rainfall was:" + avg + " inches");
    System.out.print("The month with the most rain was: ");


       }
    private static void getMaxValue(double[] rain) {


    getMaxValue(rain);
    System.out.println(getMaxValue(rain));


    System.out.println("The month with the least rain was: ");
    }
    private static void getMinValue(double[] rain) {

    getMinValue(rain);
    System.out.println(getMaxValue(rain));



       }}


我已经准备好大部分。我只是想知道如何从输入的数字中获取“最大”和“最小”。

任何帮助将是巨大的!

最佳答案

您可以通过循环数组找到max或min。并将返回类型void更改为double,这样方法将返回max rain;

private static double getMaxValue(double[] rain) {

    double max=0;
    for(double i : rain){
       if(i>max){
         max=i;
       }
    }
    return max;
}


并用作

System.out.println(getMaxValue(rain));


同样的分钟

private static double getMinValue(double[] rain) {

    double min=Double.MAX_VALUE;
    for(double i : rain){
       if(i<min){
         min=i;
       }
    }
    return min;
}


但是在您的代码中有很多错误

1)

double rain[]=new double[13];


这应该是

double rain[]=new double[12];


因为这是数组长度,所以您有12个月的时间。

2)你错过了

rain [3]


3)您分配给13的索引应该是12。

rain [13] = kenny.nextDouble();  -->  rain [12] = kenny.nextDouble();


因此,这是完整的示例。

public class apples {

    public static void main(String[] args) {
        Scanner kenny = new Scanner(System.in);
        double rain[] = new double[12];
        double sum = 0;
        double avg = 0;
        double most = 0;
        double least = 0;

        System.out.println("Your local weather man here getting paid to tell you the wrong weather!!");
        System.out.println("");
        System.out.println("Please enter in the following rainfall for the months ahead: ");
        System.out.println("Month\tRainfall (In inches)");
        System.out.print("January: ");
        rain[0] = kenny.nextDouble();
        System.out.print("February: ");
        rain[1] = kenny.nextDouble();
        System.out.print("March: ");
        rain[2] = kenny.nextDouble();
        System.out.print("April: ");
        rain[3] = kenny.nextDouble();
        System.out.print("May: ");
        rain[4] = kenny.nextDouble();
        System.out.print("June: ");
        rain[5] = kenny.nextDouble();
        System.out.print("July: ");
        rain[6] = kenny.nextDouble();
        System.out.print("August: ");
        rain[7] = kenny.nextDouble();
        System.out.print("September: ");
        rain[8] = kenny.nextDouble();
        System.out.print("October: ");
        rain[9] = kenny.nextDouble();
        System.out.print("November: ");
        rain[10] = kenny.nextDouble();
        System.out.print("December: ");
        rain[11] = kenny.nextDouble();

        //(Or rain[] = 1,2,3,4,5,6,7,8,9,10,11,12);
        sum = rain[0] + rain[1] + rain[2] + rain[3] + rain[4] + rain[5] + rain[6] + rain[7] + rain[8] + rain[9] + rain[10] + rain[11];
        avg = sum / 12;

        System.out.println("The sum of all the rain is: " + sum);
        System.out.println("The average rainfall was:" + avg + " inches");
        most =getMaxValue(rain);
        least=getMinValue(rain);
        System.out.println("The max rain is: " + most);
        System.out.println("The min rain is: " + least);

    }

    private static double getMaxValue(double[] rain) {
        double max = 0;
        for (double i : rain) {
            if (i > max) {
                max = i;
            }
        }
        return max;
    }

    private static double getMinValue(double[] rain) {

        double min = Double.MAX_VALUE;
        for (double i : rain) {
                        System.out.println(i);
            if (i < min) {
                min = i;
            }
        }
        System.out.println(min);
        return min;
    }
}


但是您可以使用包含所有月份的数组。这样做的好处是可以动态循环而不是硬接线。当输入为负数时,您可以发出警告。

public class apples {

    public static void main(String[] args) {
        Scanner kenny = new Scanner(System.in);
        double rain[] = new double[12];
        double sum = 0;
        double avg = 0;
        double most = 0;
        double least = 0;

        System.out.println("Your local weather man here getting paid to tell you the wrong weather!!");
        System.out.println("");
        System.out.println("Please enter in the following rainfall for the months ahead: ");
        System.out.println("Month\tRainfall (In inches)");

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

        for (int i=0;i<months.length;i++) {
            System.out.println(months[i]+" :");
            double val = kenny.nextDouble();
            while(val<0){
                System.out.println("negatives not allowed ! enter again");
                val = kenny.nextDouble();
            }
            rain[i]=val;
            sum+=val;
        }

        avg = sum / 12;

        System.out.println("The sum of all the rain is: " + sum);
        System.out.println("The average rainfall was:" + avg + " inches");
        most =getMaxValue(rain);
        least=getMinValue(rain);
        System.out.println("The max rain is: " + most);
        System.out.println("The min rain is: " + least);

    }

    private static double getMaxValue(double[] rain) {
        double max = 0;
        for (double i : rain) {
            if (i > max) {
                max = i;
            }
        }
        return max;
    }

    private static double getMinValue(double[] rain) {

        double min = Double.MAX_VALUE;
        for (double i : rain) {
                        System.out.println(i);
            if (i < min) {
                min = i;
            }
        }
        System.out.println(min);
        return min;
    }
}

10-04 17:54