返回的值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);
}
}