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