package com.array;
//学生类
public class Student {
public int number;//学号
public int state;//年级
public int score;//成绩
//显示学生信息的方法
public String info(){
return "学号:" +number+",年级:"+state+",成绩:"+score;
}
}
//测试类
package com.array;
public class StudentTest {
public static void main(String[] args) {
//创建20个学生对象
Student[] s = new Student[20];
for (int i = 0; i < s.length; i++) {
//给元素赋值
s[i] = new Student();
//给Student对象的属性赋值
s[i].number = (i + 1);
s[i].state = (int) (Math.random() * (6 - 1 + 1) + 1);
s[i].score = (int)(Math.random()*(100-0+1));
}
System.out.println("----------------");
//打印三年级的学生信息
for (int i = 0; i s[j+1].score) {
//如果需换序,交换的是数组元素,Student对象
Student temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
}
//遍历学生数组
for (int i = 0; i < s.length; i++) {
System.out.println(s[i].info());
}
}
}