计算温度与湿度
计算温度与湿度
代码如下:
package Day05;
import java.util.Scanner;
public class Weather {
public static void main (String[] args) {
final int NUMBER_OF_DAYS = 10;
final int NUMBER_OF_HOURS = 2;
double[][][] data = new double[NUMBER_OF_DAYS][NUMBER_OF_HOURS][2];
Scanner input = new Scanner(System.in);
for (int k = 1; k <= NUMBER_OF_DAYS; k++) {
for (int j = 1; j <= NUMBER_OF_HOURS; j++) {
System.out.println("please enter the temperature of day: "
+ k + " and hour:" + j);
if (input.hasNextDouble()) {
data[k - 1][j - 1][0] = input.nextDouble();//
}
System.out.println("please enter the humidity of day: "
+ k + " and hour:" + j);
if (input.hasNextDouble()) {
data[k - 1][j - 1][1] = input.nextDouble();
}
}
}
input.close();
for (int i = 0; i < NUMBER_OF_DAYS; i++) {
double dayTemTotal = 0, dailyHumTotal = 0;
for(int j = 0; j < NUMBER_OF_HOURS; j++) {
dayTemTotal += data[i][j][0];
dailyHumTotal += data[i][j][1];
}
System.out.println("Day " + i
+ "'s average temperature is " + dayTemTotal/NUMBER_OF_HOURS);
System.out.println("Day " + i
+ "'s average humidity is " + dailyHumTotal/NUMBER_OF_HOURS);
}
}
}