【问题标题】:Finding the largest value and the smallest in my code in Java在我的 Java 代码中查找最大值和最小值
【发布时间】:2017-03-19 12:20:23
【问题描述】:

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

用户应该能够输入:

周数 该人在一周内测量了多少次体温。 每周的温度。

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

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

这是我已经走了多远:

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


}

}

【问题讨论】:

  • 导入 java.util.*; //Scanner, Locale class Temperatures { public static void main (String[] args) 应该在代码的开头
  • 具体来说,我只需要一些帮助来开始了解如何在我的代码中找到最低温度。
  • 你可以看看DoubleSummaryStatistics

标签: java arrays sorting max minimum


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    将数组放入流中,使用 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() 方法。

    【讨论】:

      【解决方案3】:

      你可以声明:

      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];
      ...
      

      【讨论】:

        【解决方案4】:

        我现在查看了此处发布的答案并将其写入我的代码:

        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 的值,我的代码将把这个数字作为所有星期的最小值。

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

        【讨论】:

          猜你喜欢
          • 2017-04-20
          • 2015-03-08
          • 2016-11-12
          • 2012-09-16
          • 2013-11-12
          • 1970-01-01
          • 2020-09-12
          • 2015-02-01
          • 2018-05-03
          相关资源
          最近更新 更多