在Student类中有两个属性(学生成绩和id),在另一个Bubblesort类中建立一个数组,数组中为Student中属性的值,使用冒泡排序将学生的成绩进行冒泡排序,并输出学生的id。(不知道哪里错了呀,请大佬解答一下)
一、Student类
package assignment2;
public class Student {
int Grade;
String studentID;
public void setGrade(int Grade){
this.Grade = Grade;
}
public int getGrade(){
return Grade;
}
public void setStudentID(String studentID){
this.studentID = studentID;
}
public String getStudentID(){
return studentID;
}
}
二、Bubblesort类
package assignment2;
public class BubbleSort {
public static void BubbleSort(Student[] stu){
if(stu == null || stu.length == 1){
return;
}
for(int i=0;i<stu.length-1;i++){
boolean ifSorted = true;
for(int j=0;j<stu.length-1-i;j++){
Student temp;
if(stu[j].getGrade()>stu[j+1].getGrade()){
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
ifSorted = false;
}
}
if(ifSorted) return;
System.out.println("排序后: ");
for(int j=0;j<stu.length;j++){
System.out.println(stu[j].getStudentID());
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student[] stu = new Student[5];
stu[0].setStudentID("001");
stu[1].setStudentID("002");
stu[2].setStudentID("003");
stu[3].setStudentID("004");
stu[4].setStudentID("005");
stu[0].setGrade(89);
stu[1].setGrade(92);
stu[2].setGrade(73);
stu[3].setGrade(65);
stu[4].setGrade(86);
System.out.print("排序前:");
for(int j=0;j<stu.length;j++){
System.out.println(stu[j].getStudentID());
}
BubbleSort(stu);
}
}