/* 使用二维数组表示一组学生的各科成绩,计算所有学生的平均分 @authur:tangxi.zq */ public class Average { public static void main(String[]args) { int [][] scores = { {82, 90, 91}, {68, 72, 64}, {95, 91, 89}, {67, 52, 60}, {79, 81, 85}, }; double average = 0 ; double sum = 0; int len = 0; for (int[]temp:scores) { len += temp.length; for(int n : temp) { sum += n; } } average = sum / len ; System.out.println("学生的平均成绩为:" + (average)); //test if (Math.abs(average - 77.733333) < 0.000001) { System.out.println("测试成功"); } else { System.out.println("测试失败"); } } }
Sign in to make a reply
用户5527594890