import java.util.List; import java.util.ArrayList; public class paixubiancheng3 { public static void main(String[] args) { StudentClass sClass = new StudentClass(); sClass.createClass(); System.out.println("Original Order:"); System.out.println(sClass.output()); sClass.sort(); System.out.println("Sorted Order:"); System.out.println(sClass.output()); } } class Student{ private String name; private double grade; private int age; public Student(String name,int age,double grade){ this.name = name; this.age = age; this.grade = grade; } public void setname(){ this.name = name; } public void setgrade(){ this.grade = grade; } public void setage(){ this.age = age; } public String getname(){ return name; } public double getgrade(){ return grade; } public int getage(){ return age; } } class StudentClass{ private int size; private List<Student> stuList; public StudentClass(){ size = 0; stuList = null; } public void createClass(){ String names[] = {"Tom","Jerry","Snoopy","Mary","Rose"}; double grades[] = {67,78.5,98,76.5,90}; int ages[] = {17,18,18,19,17}; size = names.length; stuList = new ArrayList<Student>(); Student temp; for (int i = 0; i<size ;i++ ) { temp = new Student(names[i],ages[i],grades[i]); stuList.add(temp); } } public void sort(){ Student temp; for(int i=0;i<size;i++){ for(int j=1;j<size;j++){ if(stuList.get(j-1).getage()>stuList.get(j).getage()){ temp=stuList.get(j-1); stuList.set(j-1,stuList.get(j)); stuList.set(j,temp); } else if (stuList.get(j-1).getage()=stuList.get(j).getage()){ if(stuList.get(j-1).getname().compareTo(stuList.get(j).getname())>0){ temp=stuList.get(j-1); stuList.set(j-1,stuList.get(j)); stuList.set(j,temp); } } else{} } } } public String output(){ StringBuilder studentInfo = new StringBuilder(); for(Student stu : stuList){ studentInfo.append("Age: "+stu.getage()+"\tName: "+stu.getname()+"\r\n"); } studentInfo.append("total: "+size+" students\n"); return studentInfo.toString(); } public void add(Student s){ stuList.add(s); size = stuList.size(); } }这段代码有错吗?如果有请修改
时间: 2023-07-15 14:11:46 浏览: 122
有几处错误,以下是修改后的代码:
```
import java.util.List;
import java.util.ArrayList;
public class PaixuBiancheng3 {
public static void main(String[] args) {
StudentClass sClass = new StudentClass();
sClass.createClass();
System.out.println("Original Order:");
System.out.println(sClass.output());
sClass.sort();
System.out.println("Sorted Order:");
System.out.println(sClass.output());
}
}
class Student {
private String name;
private double grade;
private int age;
public Student(String name, int age, double grade){
this.name = name;
this.age = age;
this.grade = grade;
}
public void setName(String name){
this.name = name;
}
public void setGrade(double grade){
this.grade = grade;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public double getGrade(){
return grade;
}
public int getAge(){
return age;
}
}
class StudentClass {
private int size;
private List<Student> stuList;
public StudentClass(){
size = 0;
stuList = new ArrayList<Student>();
}
public void createClass(){
String names[] = {"Tom","Jerry","Snoopy","Mary","Rose"};
double grades[] = {67,78.5,98,76.5,90};
int ages[] = {17,18,18,19,17};
size = names.length;
Student temp;
for (int i = 0; i<size ;i++ ) {
temp = new Student(names[i],ages[i],grades[i]);
stuList.add(temp);
}
}
public void sort(){
Student temp;
for(int i=0;i<size;i++){
for(int j=1;j<size-i;j++){
if(stuList.get(j-1).getAge()>stuList.get(j).getAge()){
temp=stuList.get(j-1);
stuList.set(j-1,stuList.get(j));
stuList.set(j,temp);
} else if (stuList.get(j-1).getAge()==stuList.get(j).getAge()
&& stuList.get(j-1).getName().compareTo(stuList.get(j).getName())>0){
temp=stuList.get(j-1);
stuList.set(j-1,stuList.get(j));
stuList.set(j,temp);
}
}
}
}
public String output(){
StringBuilder studentInfo = new StringBuilder();
for(Student stu : stuList){
studentInfo.append("Age: "+stu.getAge()+"\tName: "+stu.getName()+"\r\n");
}
studentInfo.append("total: "+size+" students\n");
return studentInfo.toString();
}
public void add(Student s){
stuList.add(s);
size = stuList.size();
}
}
```
主要的修改有:
1. 类名 PaixuBiancheng3 首字母应该大写。
2. Student 类中的 setter 方法没有传入参数。
3. StudentClass 类中创建 List 对象时没有初始化,应该改为 `stuList = new ArrayList<Student>();`。
4. sort 方法中的比较条件有误,应该使用 `>` 或 `<` 进行比较。
5. sort 方法中比较年龄相同的情况下,应该再比较姓名的字典序。
阅读全文