运行效果如下
示例代码如下:
public class Test {
public static void main(String[] args) {
List<List<Double>> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
List<Double> scores = new ArrayList<>();
students.add(scores);
for (int j = 0; j < 4; j++) {
System.out.printf("请输入第%s位学生的第%s门课程的成绩:", (i + 1), (j + 1));
scores.add(scanner.nextDouble());
}
}
for (int i = 0; i < students.size(); i++) {
System.out.printf("第%s位学生的课程成绩:", i + 1);
double total = 0;
for (int j = 0; j < students.get(i).size(); j++) {
Double score = students.get(i).get(j);
total += score;
System.out.print(score + "\t");
}
double avg = total / students.get(i).size();
System.out.print("总成绩:" + total + "\t");
System.out.print("平均值为:" + avg + "\t\n");
}
}
}
运行效果如下: