建立一个学生类,其中成员变量为学号、姓名和五门课程成绩;在测试类中定义2个学生类的对象,分别对这2

建立一个学生类,其中成员变量为学号、姓名和五门课程成绩;在测试类中定义2个学生类的对象,分别对这2Java建立一个学生类,其中成员变量为学号、姓名和五门课程成绩;在测试类中定义2个学生类的对象,分别对这2个学生五门课程成绩排序(升序),并输出排序后的成绩。 五门课程成绩利用数组表示。 谢谢

第一步创建学生类
第二步创建测试类

//学生类
public class Student {

//成员变量 学号
private String number;

//成员变量 姓名
private String name;

//英语成绩
private int englishScore;

//数学成绩
private int mathScore;

//物理成绩
private int physicsScore;

//语文成绩
private int languageScore;

//化学成绩
private int chemistryScore;

//空参
public Student() {
super();
// TODO Auto-generated constructor stub
}

//有参
public Student(String number, String name, int englishScore, int mathScore, int physicsScore, int languageScore,
int chemistryScore) {
super();
this.number = number;
this.name = name;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.physicsScore = physicsScore;
this.languageScore = languageScore;
this.chemistryScore = chemistryScore;
}

//set get 方法
public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getEnglishScore() {
return englishScore;
}

public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}

public int getMathScore() {
return mathScore;
}

public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}

public int getPhysicsScore() {
return physicsScore;
}

public void setPhysicsScore(int physicsScore) {
this.physicsScore = physicsScore;
}

public int getLanguageScore() {
return languageScore;
}

public void setLanguageScore(int languageScore) {
this.languageScore = languageScore;
}

public int getChemistryScore() {
return chemistryScore;
}

public void setChemistryScore(int chemistryScore) {
this.chemistryScore = chemistryScore;
}
}
//测试类
public class TestStudent {

public static void main(String[] args) {
//定义两个学生对象 student1 student2
Student student1 = new Student("001", "zs", 99, 78, 60, 70, 69);
Student student2 = new Student("002", "ls", 100, 88, 60, 70, 69);
//调用方法 把student1存入数组里面
int[] list1 = addScoreList(student1);
//调用方法 把student2存入数组里面
int[] list2 = addScoreList(student2);

System.out.println("student1五门课程成绩排序(升序):");
//进行升序输出
Arrays.sort(list1); // 升序排列方法
//循环遍历成绩
for (int i = 0; i < list1.length; i++) {

System.out.println(list1[i]);

}

System.out.println("student2五门课程成绩排序(升序):");
//进行升序输出
Arrays.sort(list2); // 升序排列方法
//循环遍历成绩
for (int i = 0; i < list2.length; i++) {

System.out.println(list2[i]);

}

}

//把学生的成绩分别存入数组里面 ---创建方法 方便调用
public static int[] addScoreList(Student student){
// 创建数组
int n = 5;
int[] a = { student.getEnglishScore(), student.getMathScore(),
student.getLanguageScore(), student.getPhysicsScore(), student.getChemistryScore() };

return a;
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-06-08
八能者堪制包裹谈
相似回答