题目要求:定义的学生类创建5个对象, 属性可为任意值. 编程对这5个对象按成绩排序, 并将结果输出。
class student
{
String name;
int age;
int result;
student(String name,int age,int result)
{
this.name = name;
this.age = age;
this.result = result;
System.out.println("姓名为:"+name+",年龄为:"+age+",成绩为:"+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 int getResult()
{
return result;
}
public void setResult(int result)
{
this.result = result;
}
}
public class test
{
public static void main(String[] args)
{
student[] ss = new student[10];
ss[1] = new student("张三",21,34);
ss[2] = new student("李四",24,65);
ss[3] = new student("王五",22,99);
ss[4] = new student("赵六",19,87);
ss[5] = new student("钱七",20,45);
sorted(ss); //该步我要调取下边的排序方法,可是老出错,括号中的参数是不是写错了啊?
}
public static void sorted(student[] stu) //排序方法
{
int temp;
for(int x = 0;x<stu.length-1;x++)
{
for(int y = 0;y<stu.length-x-2;y++)
{
if(stu[y].result<stu[y+1].result)
{
temp = stu[y].result;
stu[y].result = stu[y+1].result;
stu[y+1].result = temp;
}
}
}
for(int i = 0;i<stu.length;i++)
{
System.out.println("姓名为:"+stu.name+",年龄为:"+stu.age+",成绩为:"+stu.result);
}
}
}