【程式語言】JAVA的第五堂課 |在陣列中取平均溫度、最大溫度、最小溫度、各日平均
Published:
by .
public class ch6_8 {
public static void main(String[] args) {
double[][] num = {{18.2,17.3,15.0,13.4},
{23.8,25.1,20.6,17.8},
{20.6,21.5,18.4,15.7}};
double temp[] = new double[3];
double sum = 0.00;
String[] day = {"時段一","時段二","時段三"};
System.out.println("\t星期一\t星期二\t星期三\t星期四");
for (int i = 0 ; i < num.length ; i++) {
System.out.print(day[i] + "\t");
for (int j = 0 ; j < num[1].length ; j++) {
System.out.print(num[i][j] + "\t" );
}
System.out.println();
}
System.out.println();
for (int i = 0 ; i < num.length ; i++) { // 印出平均溫度
System.out.print(day[i]);
for (int j = 0 ; j < num[1].length ; j++) {
sum += num[i][j];
}
temp[i] += sum;
System.out.printf(" 的平均溫度為:%.2f \n" , (sum/num[1].length));
sum = 0;
}
System.out.println();
float total = 0;
for (int i = 0 ; i < num.length-1 ; i++) { // 印出各時段平均溫度
for (int j = 0 ; j < num[1].length-1 ; j++) {
total += num[j][i];
}
System.out.printf(day[i] + "\t %.2f \n" ,(total/num.length));
}
double max = num[0][0] ;
double min = num[0][0];
int index = 0;
for (int i = 0 ; i < num.length ; i++) { // 印出平均溫度
for (int j = 0 ; j < num[1].length ; j++) {
if (num[i][j] > max) {
index = i;
max = num[i][j];
}
else {
index = i;
min = num[i][j];
}
}
}
System.out.println();
System.out.println(day[index-1] + "\t" + max);
System.out.println(day[index-1] + "\t" + min);
}
}