在基础题里有个对象排序问题,我写了段代码,感觉好像不行,请帮我看,是否可以改进,让内容更加符合题目要求,谢谢!
/*
5、定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性. 属性(成员变量)需
要私有并提供get, set方法, 可以通过构造函数进行初始化。
6、使用第5题定义的学生类创建5个对象, 属性可为任意值. 编程对这5个对象按成绩排
序, 并将结果输出。
*/
class Student
{
private String name;
private int age;
private int score;
Student(String name,int age,int score)
{
this.name = name;
this.age = age;
this.score = score;
}
public void setName(String name,int age,int score)
{
this.name = name;
this.age = age;
this.score = score;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int getScore()
{
return score;
}
Student(Student s1,Student s2,Student s3,Student s4,Student s5)
{
int[] arr = {s1.score,s2.score,s3.score,s4.score,s5.score};
for(int x = 0; x < arr.length-1; x++)
{
for(int y = x+1; y < arr.length; y++)
{
if(arr[x]>arr[y])
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
for(int z= 0;z < arr.length; z++)
{
if(z!=arr.length-1)
System.out.print(arr[z]+",");
else
System.out.print(arr[z]);
}
}
}
class Sort
{
void Sort()
{
}
}
class StudentDemo
{
public static void main(String[] args)
{
Student s1 = new Student("zs",15,100);
Student s2 = new Student("ls",15,80);
Student s3 = new Student("ww",15,97);
Student s4 = new Student("zl",15,65);
Student s5 = new Student("zh",15,76);
new Student(s1,s2,s3,s4,s5);
}
}
/*
运行结果:
65,76,80,97,100
*/ |
|