例如:
package day.ever;
public class StudentTest1 {
public static void main(String[] args) {
Student1 []stus=new Student1[20];
for(int i=0;i<stus.length;i++) {
stus[i]=new Student1();
stus[i].number=(i+1);
stus[i].state=(int)(Math.random()*(6-1+1)+1);
stus[i].score=(int)(Math.random()*(100-0+1)+0);
System.out.println(stus[i].info());
}
StudentTest1 test=new StudentTest1();
System.out.println("**************************");
test.search(stus,3);
System.out.println("**************************");
test.sort(stus);
public void search(Student1 []stus,int state) {
for(int i=0;i<stus.length;i++) {
if(stus[i].state==state)
System.out.println(stus[i].info());
}
}
public void sort(Student1[] stus) {
for(int i=0;i<stus.length-1;i++) {
for(int j=0;j<stus.length-1-i;j++) {
if(stus[j].score>stus[j+1].score){
Student1 stemp=stus[j];
stus[j]=stus[j+1];
stus[j+1]=stemp;
}
}
}
for(int i=0;i<stus.length-1;i++)
System.out.println(stus[i].info());
}
}
}
class Student1{
int number;
int state;
int score;
public String info() {
return "学号:"+number+",年级:"+state+",成绩:"+score;
}
}
(红字体的为另外两种方法的定义,不能定义在main方法中,应将该方法的定义放在下一行}的后面)