返回的值return;需要接收
例题
public class Student {
int number;
int state;
int score;
public String info() {
return "学号:" + number + ",年级:" + state + ",成绩:" + score;
}
public void print(Student[] stu) {
// 遍历学生数组
for (int i = 0; i < stu.length; i++) {
// System.out.println(stu[i]);//引用类型的变量不是null,
// 就是存的地址值
System.out.println(stu[i].info());
}
}
/**
* 给student数组排序
*
* @param args
*/
public void sort(Student[] stu) {
// 使用冒泡排序把学生成绩排序,并遍历学生所有信息
for (int ii = 0; ii < stu.length - 1; ii++) {
for (int jj = 0; jj < stu.length - 1 - ii; jj++) {
if (stu[jj].score < stu[jj + 1].score) {
// 如果需要换顺序,交换的是数组的元素,Student对象
Student temp = stu[jj];
stu[jj ] = stu[jj+ 1];
stu[jj+ 1] = temp;
}
}
}
}
/**
* 打印查找学生信息
* @param args
*/
public void searchStrate(Student[] stu,int state){
//打印三年级的学生信息
for(int i=0;i<stu.length;i++){
if(stu[i].state==state ){
System.out.println(stu[i].info());
}
}
}
public static void main(String[] args) {
Student[] stu1=new Student[20];
for(int i=0;i<stu1.length;i++){
//给数组元素赋值
stu1[i]=new Student();
//给student对象的属性赋值
stu1[i].number= (i+1);
//年级【1,6】
stu1[i].state=(int)(Math.random()*(6-1+1))+1;
stu1[i].score =(int)(Math.random()*(100-0+1));
}
Student stu=new Student();
stu.print(stu1);
System.out.println("*****");
stu.searchStrate(stu1, 3);
stu.sort(stu1);
stu.print(stu1);
}
}