在主函数中定义一个数组student[1000],用于记录N个学生的学号.

2020-10-28 教育 81阅读
闲着无聊 送你了
package test;
public class Student {
private String name;
private int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package test;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {

Student [] studentInfo=new Student[5];
studentInfo[0]=new Student("张", 10);
studentInfo[1]=new Student("张", 9);
studentInfo[2]=new Student("李", 3);
studentInfo[3]=new Student("王1", 7);
studentInfo[4]=new Student("王2", 88);
Student [] newStudentInfo=Main.selectSort(studentInfo);
for(int i=0;i System.out.println(newStudentInfo[i].getName()+":"+newStudentInfo[i].getAge());
}
}
public static Student [] selectSort(Student [] studentInfo){//选择排序算法
for (int i=0;i int min=i;
for (int j=i+1;j if (studentInfo[min].getAge()>studentInfo[j].getAge()){
min=j;
}
}
if (min!=i){
Student temp=studentInfo[i];
studentInfo[i]=studentInfo[min];
studentInfo[min]=temp;
}
}
return studentInfo;
}
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com