有三十个学生,每个学生有学号、姓名和4门成绩(学生信息随机产生,存入一个对象数组),输出一个学生观的信息
时间: 2023-12-15 12:48:51 浏览: 92
以下是一个示例代码,用于随机生成学生信息并输出其中一个学生的信息:
```java
import java.util.Random;
public class Student {
int id;
String name;
int[] grades = new int[4];
public Student(int id, String name, int[] grades) {
this.id = id;
this.name = name;
this.grades = grades;
}
public static void main(String[] args) {
Student[] students = new Student[30];
Random rand = new Random();
for (int i = 0; i < students.length; i++) {
int id = i + 1;
String name = "Student" + id;
int[] grades = new int[4];
for (int j = 0; j < grades.length; j++) {
grades[j] = rand.nextInt(101);
}
students[i] = new Student(id, name, grades);
}
// 输出第一个学生的信息
Student s = students[0];
System.out.println("学号:" + s.id);
System.out.println("姓名:" + s.name);
System.out.print("成绩:");
for (int i = 0; i < s.grades.length; i++) {
System.out.print(s.grades[i] + " ");
}
}
}
```
输出结果:
```
学号:1
姓名:Student1
成绩:96 57 31 83
```
阅读全文