在Java中查找代码中的最大值和最小值

时间:2016-11-05 21:35:39

标签: java arrays sorting max minimum

我正在创建一个代码,让用户输入不同类型的温度。

用户应该可以输入:

周数 这个人在一周内测量温度的时间。 每周的温度。

当用户输入信息时,程序应显示已输入的温度。

程序应该每周显示最低温度。 该计划应该每周显示最高温度。 该计划应在所有周内显示最低和最高温度。 该计划应显示每周和整个星期的平均温度。

这就是我的到来:

{
    System.out.println ("Temperatures\n");


    Scanner    in = new Scanner (System.in);
    in.useLocale (Locale.US);

    //Here is where information is being put in
    System.out.print ("amount of weeks: ");
    int    amountOfWeeks = in.nextInt ();
    System.out.print ("measuring temperature per week: ");
    int    measurementsPerWeek = in.nextInt ();

    //array to store the information
    double[][]    t = new double[amountOfWeeks + 1][measurementsPerWeek + 1];

    //mata in temperaturerna
    for (int week = 1; week <= amountOfWeeks; week++)
    {
        System.out.println ("temperatures - week " + week + ":");
        for (int measurement = 1; measurement <= measurementsPerWeek; measurement++)
            t[week][measurement] = in.nextDouble ();
    }
    System.out.println ();

    //show the temperatures
    System.out.println ("temperatures");
    for (int week = 1; week <= amountOfWeeks; week++)
    {
        for (int measurement = 1; measurement <= measurementsPerWeek; measurement++)
            System.out.print (t[week][measurement] + " ");

        System.out.println ();
        }

    //lowest, highest, the sum of all temperatures, and average temp per week
    double[]    minT = new double[amountOfWeeks + 1];
    double[]    maxT = new double[amountOfWeeks + 1];
    double[]    sumT = new double[amountOfWeeks + 1];
    double[]    averageT = new double[amountOfWeeks];

    // write the code to find the lowest temperature for the first week



    //´Lowest, highest, sum, average for all weeks
    double    minTemp = minT[1];
    double    maxTemp = maxT[1];
    double    sumTemp = sumT[1];
    double    averageTemp = 0;


}

}

4 个答案:

答案 0 :(得分:0)

在列表中查找最大值的一般算法是迭代列表并存储到目前为止遇到的最大值。例如,如果您有10个数字存储在整数数组中:

int max=intArray[0];
for (int i=1; i<10;i++)
         if (intArray[i]>max)
             max=intArrray[i];

答案 1 :(得分:0)

将数组放入流中并使用min()方法[或max()或average()]来获取OptionalDouble值。

OptionalDouble minValue = Arrays.stream(myArray).min();

然后,如果您只想打印它,请将ifPresent()与lambda表达式或方法引用一起使用:

minValue.ifPresent(e -> System.out.println(e));

minValue.ifPresent(System.out::println);

或者如果你想做其他事情,你可以使用get()方法。

答案 2 :(得分:0)

您可以声明:

double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;

每当你读到温度时,你会测试它是否为新的最大值或最小值,并在必要时更新它们,如下所示:

...
t[week][measurement] = in.nextDouble ();
if (t[week][measurement] < min) min = t[week][measurement];
if (t[week][measurement] > max) max = t[week][measurement];
...

答案 3 :(得分:0)

我现在回顾了这里发布的答案并将其写入我的代码:

double min = Double.MAX_VALUE;

for (int week = 1; week<= amountOfWeeks ; week++)
{
    for (int measurement = 1; measurement <= measurementsPerWeek; measurement ++)
         if (t[week][measurement] < min)
         min = t[week][measurement];

         System.out.println ("min is:" + min);
     }

现在代码遍历所有周的所有输入的输入数据,并找到最小数字并打印出来。所以基本上,如果第一周的第一个温度是1,并且用户没有输入任何低于1的温度,我的代码将把这个数字作为所有周的最小值。

我怎样才能改变它,以便它自己找到每周的最小输入?