本帖最后由 刘基军 于 2012-4-30 08:40 编辑
1.
student[] ss = new student[10]; //你的student数组ss[0],ss[6],ss[7],ss[8],ss[9]都是null,而你下边有使用到它们了,会报空指针异常
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);
2.
for(int x = 0;x<stu.length-1;x++)
{
for(int y = 0;y<stu.length-x-1;y++) //冒泡排序法:这边的y应该是: y<stu.length-x-1
{
if(stu[y].result<stu[y+1].result)
{
temp = stu[y].result;
stu[y].result = stu[y+1].result;
stu[y+1].result = temp;
}
}
}
3.
System.out.println("姓名为:"+stu.name+",年龄为:"+stu.age+",成绩为:"+stu.result); //你写成了直接用数组名,会报错
4.你这样做,并不是排序效果,而是实现了篡改学生成绩的目的,呵呵,题目的本意应该是让你使用集合来实现排序的...- class test
- {
- public static void main(String[] args)
- {
- student[] ss = new student[5];
- ss[0] = new student("张三",21,34);
- ss[1] = new student("李四",24,65);
- ss[2] = new student("王五",22,99);
- ss[3] = new student("赵六",19,87);
- ss[4] = 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-1;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[i].name+",年龄为:"+stu[i].age+",成绩为:"+stu[i].result);
- }
- }
- }
复制代码 |