如何输入仅当输入“年”时才能打印的最小值和最大值?

时间:2018-12-13 22:28:21

标签: java

创建程序,用户可以输入一个月并查看该月的平均温度。用户可以输入“年”,并查看每个月的所有月份和平均温度,以及年度平均值以及最高和最低的每月平均值。到目前为止,我在最大/最低平均值方面遇到困难。输入月份时,它正在打印,但不应该。我试图将代码移到不同的位置,但这似乎无关紧要,因为在输入年份或月份时它仍然会打印。

import java.util.Scanner;
import java.util.*;

public class MonthlyTemp {

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);

        //set up arrays//
        final int NUM_MONTHS = 12;                      
          int [] avgMonthTemp = new int [NUM_MONTHS];
          String [] monthName = new String[NUM_MONTHS];        
          int i = 0;
          String userMonthName ="";

          monthName[0] = "January";
          monthName[1] = "February";
          monthName[2] = "March";
          monthName[3] = "April";
          monthName[4] = "May";
          monthName[5] = "June";
          monthName[6] = "July";
          monthName[7] = "August";
          monthName[8] = "September";
          monthName[9] = "October";
          monthName[10] = "November";
          monthName[11] = "December";

          avgMonthTemp[0] = 75;
          avgMonthTemp[1] = 75;
          avgMonthTemp[2] = 79;
          avgMonthTemp[3] = 82;
          avgMonthTemp[4] = 85;
          avgMonthTemp[5] = 86;
          avgMonthTemp[6] = 90;
          avgMonthTemp[7] = 90;
          avgMonthTemp[8] = 90;
          avgMonthTemp[9] = 86;
          avgMonthTemp[10] = 81;
          avgMonthTemp[11] = 77;

          System.out.println("Enter a month or the word year: ");
          userMonthName = scnr.nextLine();


           for(i=0; i < NUM_MONTHS; i++) {
             if(userMonthName.equalsIgnoreCase("year")) {    
             System.out.println(monthName[i] + " " + avgMonthTemp[i]);
             }
           }

           for(i=0; i < NUM_MONTHS; i++) {
             if(userMonthName.equalsIgnoreCase(monthName[i])) {
                 System.out.println(monthName[i] + " " + avgMonthTemp[i]);
             }
           }
           Arrays.sort(avgMonthTemp);
           System.out.println("Minimum = " + avgMonthTemp[0]);
           System.out.println("Maximum = " + avgMonthTemp[avgMonthTemp.length-1]);  
       }
}

任何指导将不胜感激! 谢谢。

2 个答案:

答案 0 :(得分:0)

Min / Max周围没有if子句,因此它始终运行。

if( userMonthName.equalsIgnoreCase("year") )
{
    // User entered "year" - print out all values
    for( i=0; i < NUM_MONTHS; i++ ) 
    {
         System.out.println( monthName[i] + " " + avgMonthTemp[i] );
    }

     Arrays.sort( avgMonthTemp );
     System.out.println( "Minimum = " + avgMonthTemp[0] );
     System.out.println( "Maximum = " + avgMonthTemp[avgMonthTemp.length-1] ); 
}
else
{
    // Did the user enter a month name?  If so print just that
    for( i=0; i < NUM_MONTHS; i++ ) 
    {
        if ( userMonthName.equalsIgnoreCase( monthName[i] ) ) 
        {
             System.out.println( monthName[i] + " " + avgMonthTemp[i] );
        }
    }
}

答案 1 :(得分:0)

如果我对您的理解正确,那么仅当用户要求您打印全年平均值时,才希望打印最大值/最小值。当前的问题是,您没有用if语句来保护两个println语句(最大/最小),因此它们将始终打印。

我认为,执行此操作的正确方法是首先确定用户是否要求提供全年的数据并将其保存为布尔值:

boolean requestedAll = userMonthName.equalsIgnoreCase("year");

然后,您可以迭代并打印:

for(int i = 0; i < NUM_MONTHS; i++) {
    if(requestedAll || userMonthName.equalsIgnoreCase(monthName[i])) {
        System.out.println(monthName[i] + " " + avgMonthTemp[i]);
    }
}

然后,如果这是每年一次的请求,则最后只能打印最大值和最小值:

if(requestedAll) {
    Arrays.sort(avgMonthTemp);
    System.out.println("Minimum = " + avgMonthTemp[0]);
    System.out.println("Maximum = " + avgMonthTemp[avgMonthTemp.length-1]); 
}