降雨等级,从阵列中找到最大值和最小值

时间:2014-10-17 02:26:49

标签: java arrays class max min

我正在为我的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"和#34; Min"从输入的数字。

任何帮助都会很棒!!

2 个答案:

答案 0 :(得分:1)

你可以通过循环数组找到max或min。并将return类型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));

和min相同;

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;
    }
}

答案 1 :(得分:1)

Java 8有一种更容易处理数据的机制,而不需要循环或临时变量。

如果您使用的是Java 8,则可以使用以下命令:

double rain[] = {3, 2, 7, 9, 10};
double totalRainfall = Arrays.stream(rain).sum;
double maxRainfall = Arrays.stream(rain).max().getAsDouble();
double minRainfall = Arrays.stream(rain).min().getAsDouble();
double avgRainfall = Arrays.stream(rain).average().getAsDouble();

与传统方法相比,这更容易阅读和理解。