//使用第5题定义的学生类创建5个对象, 属性可为任意值.
//编程对这5个对象按成绩排序, 并将结果输出。
class Student
{
private String name;
private int age;
private double result;
Student()
{
}
Student(String name,int age,double result)
{
this.name = name;
this.age = age;
this.result = result;
}
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;
}
public double getResult()
{
return result;
}
public void setResult(double result)
{
this.result = result;
}
public void method(Student[] s)
{
Student temp=null;
for(int x = 0;x<s.length-1;x++)//为什么在程序运行的时候,这个for循环不运行????????
{
for(int y = 0;y<y-x-1;y++)
{
if(s[y].result>s[y+1].result)
{
temp = s[y];
s[y] = s[y+1];
s[y+1] = temp;
}
}
}
for(int x = 0;x<s.length;x++)
{
System.out.println(s[x].name+s[x].age+s[x].result);
}
}
}
public class StudentDemo2
{
public static void main(String[] args)
{
Student stu = new Student();
Student[] s=new Student[5];
s[0] = new Student("张三",21,87.3);
s[1] = new Student("王五",23,78.3);
s[2] = new Student("宋六",19,97.3);
s[3] = new Student("李四",24,66.3);
s[4] = new Student("马二",21,83.3);
stu.method(s);
}
}
|