| 1234567891011121314151617181920212223242526272829 |
- package p2;
- /**
- * @author WanJl
- * @version 1.0
- * @title StudentTest
- * @description
- * @create 2026/7/20
- */
- public class StudentTest {
- public static void main(String[] args) {
- //创建StudentDemo对象
- StudentDemo sd=new StudentDemo();
- //调用初始化学生的方法,得到一个数组
- Student[] students = sd.initStudents();
- //调用随机学生id的方法,获取随机数id
- int ranId=sd.randomStudent();
- //比较 ranId和学生数组students中的哪个元素的id属性相同
- for (int i = 0; i < students.length; i++) {
- Student s =students[i]; //拿取一个数组的元素赋值给学生变量名
- if (ranId==s.getId()){ //得到的随机数 和 学生的id比较,然后获取学生id对应的姓名
- System.out.println("随机到的学生是:"+s.getName());
- }
- }
- }
- }
|