Java最小值

时间:2014-03-24 13:48:24

标签: java

一切都运转良好,我只需要我的输出来说明降雨量最高/最低的季度,而不是实际值。我不确定如何将季度和值组合在一起,以便输出为第1,第2,第3或第4季。

import java.util.Scanner;

public class rainfall
{
   public static void main(String[] args) 
   { 
      Scanner scan = new Scanner(System.in); 
      double[] rainfall = new double[4]; 
      double totalRainfall = 0.0; 
      double max = 0, min = 0;


      for (int i=0; i < 4; i++) 
      { 
         System.out.print("Enter rainfall for quarter " + (i+1) + ": "); 
         rainfall[i] = scan.nextDouble(); 
         totalRainfall += rainfall[i]; 
         if (i == 0)
         {
            max = min = rainfall[i];
         }

         {
            if (rainfall[i] > max)
               max = rainfall[i];
            else if (rainfall[i] < min)
               min = (i + 1);
            //min = rainfall[i];     
         }
     }

   System.out.println("Total rainfall = "+totalRainfall); 
   System.out.println("Average rainfall = "+(totalRainfall / 4.0)); 
   System.out.println("Max quarter rainfall = "+ max);
   System.out.println("Min quarter rainfall = " + min);

   //System.out.println("Max quarter rainfall = "+ maxQuarter);
   //System.out.println("Min quarter rainfall = " + minQuarter);

   }//end main 
}//end class

3 个答案:

答案 0 :(得分:2)

只需存储索引而不是值:

if (rainfall[i] > rainfall[max])
    max = i;
else if (rainfall[i] < rainfall[min])
    min = i;

System.out.println("Max quarter rainfall = " + max + 1);
System.out.println("Min quarter rainfall = " + min + 1);

答案 1 :(得分:1)

您需要跟踪maxQuarter和minQuarter(添加为int)以及max和min。像这样:

import java.util.Scanner;

public class rainfall
{
   public static void main(String[] args) 
   { 
      Scanner scan = new Scanner(System.in); 
      double[] rainfall = new double[4]; 
      double totalRainfall = 0.0; 
      double max = 0, min = 0;
      // init to Q1 as that's the 1st tested
      int maxQuarter = 1, minQuarter = 1;


      for (int i=0; i < 4; i++) 
      { 
         System.out.print("Enter rainfall for quarter " + (i+1) + ": "); 
         rainfall[i] = scan.nextDouble(); 
         totalRainfall += rainfall[i]; 
         if (i == 0)
         {
            max = min = rainfall[i];
         }

         {
            if (rainfall[i] > max) {
               max = rainfall[i];
               maxQuarter = i + 1;
            }
            if (rainfall[i] < min) {
               min = rainfall[i];
               minQuarter = i + 1;
            }
         }
     }

   System.out.println("Total rainfall = "+totalRainfall); 
   System.out.println("Average rainfall = "+(totalRainfall / 4.0)); 
   System.out.println("Max quarter rainfall = "+ max);
   System.out.println("Min quarter rainfall = " + min);

   System.out.println("Max quarter rainfall = "+ maxQuarter);
   System.out.println("Min quarter rainfall = " + minQuarter);

   }//end main 
}//end class

通过这些更改,它可以正常运行我的基本测试:

Enter rainfall for quarter 1: 2
Enter rainfall for quarter 2: 4
Enter rainfall for quarter 3: 6
Enter rainfall for quarter 4: 7
Total rainfall = 19.0
Average rainfall = 4.75
Max quarter rainfall = 7.0
Min quarter rainfall = 2.0
Max quarter rainfall = 4
Minquarter rainfall = 1

答案 2 :(得分:0)

您应该格式化代码(在Eclipse中按Ctrl + Shift + f)。

保持i索引与min和max并行,例如imax,imin。否则你会丢失值的来源,就像你的情况一样。