0
当前我的代码返回用户输入每个月的温度值时提交的最高温度值。我怎样才能让程序以最高的条目返回月份,而不是最高的条目本身?从第二个阵列返回值
import java.util.Scanner;
public class Reader {
static String months[] =
{
"January" , "February" , "March" , "April" , "May" ,
"June", "July", "August", "September", "October", "November",
"December"
};
public static void main(String[] args){
int[] avgMonth;
int tempRecords = 0;
double tempSum = 0;
double avgTemp;
double getHotMonth;
//collect user input for avg temp and put into an array with the month
Scanner input = new Scanner(System.in);
avgMonth = new int[months.length];
for(int i=0; i < months.length; i++){
System.out.println("Enter the average temperature for the month of "+months[i]);
avgMonth[i] = input.nextInt();
}
//call avgTemp, takes array of temps as argument, return total avg for year
for(int i=0;i<months.length; i++)
tempSum += avgMonth[i];
avgTemp = tempSum/months.length;
//call getHotMonth, takes entire array as argument, find index of hottest month
getHotMonth = avgMonth[0];
for (int i=0;i<months.length;i++){
if (avgMonth[i] > getHotMonth)
getHotMonth = avgMonth[i];
}
//displayResults, display average and hottest month
//args are average and the index array number of hottest month
//final output
displayResults(avgTemp,getHotMonth);
input.close();
}//close main
public static void displayResults(double average, double getHotMonth){
System.out.println("The average temperature for the year was "+average+" degrees F with "+getHotMonth+" being the hottest month.");
}
}
印出结果离开hotMonth结果的空白,什么是从哪里来的它在最后的声明中。你知道这个问题会是什么吗? – squirrel