构造一个java程序,可以随机生成学生对象,其中学生类包含学号,姓名,年龄,班级,性别
时间: 2023-05-18 17:06:20 浏览: 120
java窗口程序,可以实现,随机选学生
好的,以下是一个可以随机生成学生对象的 Java 程序:
```java
import java.util.Random;
public class Student {
private String id;
private String name;
private int age;
private String className;
private String gender;
public Student(String id, String name, int age, String className, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.className = className;
this.gender = gender;
}
public static void main(String[] args) {
String[] names = {"张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十"};
String[] classes = {"一班", "二班", "三班", "四班", "五班"};
String[] genders = {"男", "女"};
Random random = new Random();
for (int i = 0; i < 10; i++) {
String id = String.format("%03d", i + 1);
String name = names[random.nextInt(names.length)];
int age = random.nextInt(10) + 15;
String className = classes[random.nextInt(classes.length)];
String gender = genders[random.nextInt(genders.length)];
Student student = new Student(id, name, age, className, gender);
System.out.println(student);
}
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",年龄:" + age + ",班级:" + className + ",性别:" + gender;
}
}
```
这个程序会生成 10 个随机的学生对象,并输出它们的信息。每个学生对象包含学号、姓名、年龄、班级和性别。
阅读全文