我有一个 java 项目,我被告知要编写一个程序,要求用户输入天数和温度。从那里我必须找到平均温度,高于平均温度的天数,以及从最高到最低对温度进行排序。*我已经成功地完成了所有这些事情,但是,我的教授要求我通过创建单独的方法来简化我的代码。例如,有一个方法来计算超过平均值的天数,等等。虽然我理解这个概念,但我不确定如何接近和这样做。*请帮忙?
这是我到目前为止的代码:
import java.util.Scanner;
public class NumberAboveAverage {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Enter number of days of temperature to calculate:");
Scanner keyboard = new Scanner(System. in);
int day = keyboard.nextInt();
int[] temperature = new int[day];
System.out.println("Enter " + day + " temperatures to calculate.");
for(int i=0; i<day; i++){
temperature[i] = keyboard.nextInt();
}
int sum = 0;
for(int i=0; i<day; i++){
sum = sum + temperature[i];
}
int average = sum/day;
System.out.println("The average temperature is: " + average);
int daysOver=0;
for(int i=0; i<day; i++){
if (temperature[i] > average){
daysOver++;
}
}
System.out.println("The temperature was above average for " + daysOver + " day(s).");
for (int i = 0; i < temperature.length; i++) {
int min = i;
for (int j = i; j < temperature.length; j++) {
if (temperature[j] < temperature[min])
min = j;
}
int temp;
temp = temperature[i];
temperature[i] = temperature[min];
temperature[min] = temp;
}
System.out.print("The temperatures in increasing order are: ");
for(int i=0; i<day; i++){
System.out.print(temperature[i]+" ");
}
}
}