请给我关于“有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学号,姓名,三门课成绩),计算出平均成绩,并把学号,姓名,三门课成绩及平均分输出。”的完整的Java代码
时间: 2024-01-04 19:03:00 浏览: 83
以下是完整的Java代码:
```
import java.util.Scanner;
public class StudentGrades {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] name = new String[5];
int[][] grades = new int[5][3];
int[] id = new int[5];
double[] average = new double[5];
for (int i = 0; i < 5; i++) {
System.out.print("Enter student " + (i+1) + "'s ID: ");
id[i] = input.nextInt();
System.out.print("Enter student " + (i+1) + "'s name: ");
name[i] = input.next();
System.out.print("Enter student " + (i+1) + "'s grades for three courses (separated by spaces): ");
grades[i][0] = input.nextInt();
grades[i][1] = input.nextInt();
grades[i][2] = input.nextInt();
// Calculate average grade
double sum = grades[i][0] + grades[i][1] + grades[i][2];
average[i] = sum / 3;
}
// Output results
System.out.println("ID\tName\tCourse 1\tCourse 2\tCourse 3\tAverage");
for (int i = 0; i < 5; i++) {
System.out.println(id[i] + "\t" + name[i] + "\t" + grades[i][0] + "\t\t" + grades[i][1] + "\t\t" + grades[i][2] + "\t\t" + average[i]);
}
}
}
```
该代码通过循环遍历5个学生的信息,从键盘输入学号、姓名、以及三门课的成绩,并计算出平均成绩。最后,输出学号、姓名、三门课成绩及平均分。
阅读全文