以下是一个使用Java动态初始化来存储六个学生成绩的示例代码。这个程序首先定义了一个名为Student的类,然后创建了一个Student数组,并使用for循环向数组中添加六个Student对象。每个Student对象都包含一个名字和五个成绩。最后,程序使用for循环打印出所有学生的名字和成绩。
import java.util.Scanner;
class Student {
private String name;
private double score1, score2, score3, score4, score5;
public Student(String name, double score1, double score2, double score3, double score4, double score5) {
网页链接 = name;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
this.score4 = score4;
this.score5 = score5;
}
public String getName() {
return name;
}
public void setName(String name) {
网页链接 = name;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[6]; // 创建一个包含6个Student对象的数组
for (int i = 0; i < 6; i++) { // 向数组中添加6个Student对象,每个对象都有不同的名字和成绩值。这里假设成绩值是键盘输入的。你可以根据需要修改成其他值。例如:students[i] = new Student("张三", 80, 85, 90, 88, 92); 等等。 scanner.nextLine(); // 获取名字 students[i] = new Student(scanner.nextLine(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble()); // 获取成绩并创建新的Student对象 } for (int i = 0; i < 6; i++) { // 打印所有学生的名字和成绩 System.out.println("学生" + (i + 1) + ":" + students[i].getName() + ",成绩:" + students[i].score1 + "," + students[i].score2 + "," + students[i].score3 + "," + students[i].score4 + "," + students[i].score5); } scanner.close(); // 关闭键盘输入流 } }```
import java.util.Scanner;
class Student {
private String name;
private double score1, score2, score3, score4, score5;
public Student(String name, double score1, double score2, double score3, double score4, double score5) {
网页链接 = name;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
this.score4 = score4;
this.score5 = score5;
}
public String getName() {
return name;
}
public void setName(String name) {
网页链接 = name;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[6]; // 创建一个包含6个Student对象的数组
for (int i = 0; i < 6; i++) { // 向数组中添加6个Student对象,每个对象都有不同的名字和成绩值。这里假设成绩值是键盘输入的。你可以根据需要修改成其他值。例如:students[i] = new Student("张三", 80, 85, 90, 88, 92); 等等。 scanner.nextLine(); // 获取名字 students[i] = new Student(scanner.nextLine(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble(), scanner.nextDouble()); // 获取成绩并创建新的Student对象 } for (int i = 0; i < 6; i++) { // 打印所有学生的名字和成绩 System.out.println("学生" + (i + 1) + ":" + students[i].getName() + ",成绩:" + students[i].score1 + "," + students[i].score2 + "," + students[i].score3 + "," + students[i].score4 + "," + students[i].score5); } scanner.close(); // 关闭键盘输入流 } }```