设计一个类Student,该类包括姓名,学号和成绩。设计一个方法,按照成绩从高到低顺序输出姓名,学
答案:2 悬赏:40
解决时间 2021-02-20 01:13
- 提问者网友:沉默菋噵
- 2021-02-19 16:33
设计一个类Student,该类包括姓名,学号和成绩。设计一个方法,按照成绩从高到低顺序输出姓名,学
最佳答案
- 二级知识专家网友:鱼芗
- 2021-02-19 17:08
public class Student {
private int id;//学号
private String name;//姓名
private int score;//成绩
public Student(int id, String name, int score) {
super();
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return (id + " " + name + " " + score);
}
}
import java.util.Arrays;
public class TestStudent {
static Student[] stus;
public static void sort(Student[] stus){
int len = stus.length;//学生个数
int[] a = new int[len];
//记录分数
for (int i = 0; i < len; i++) {
a[i] = stus[i].getScore();
}
Arrays.sort(a);//升序排序
for (int i = a.length - 1; i >= 0 ; i--) {
for (int j = 0; j < stus.length; j++) {
if(stus[j].getScore() == a[i]){
System.out.println(stus[j]);
}
}
}
}
public static void main(String[] args) {
stus = new Student[3];
stus[0] = new Student(1,"a",87);
stus[1] = new Student(2,"b",78);
stus[2] = new Student(3,"c",99);
System.out.println("按分数从高到低排序:");
sort(stus);
}
}按分数从高到低排序:
3 c 99
1 a 87
2 b 78
望采纳哦,亲!
追答:刚才你是不是还问了一道一模一样的题?。。。
private int id;//学号
private String name;//姓名
private int score;//成绩
public Student(int id, String name, int score) {
super();
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return (id + " " + name + " " + score);
}
}
import java.util.Arrays;
public class TestStudent {
static Student[] stus;
public static void sort(Student[] stus){
int len = stus.length;//学生个数
int[] a = new int[len];
//记录分数
for (int i = 0; i < len; i++) {
a[i] = stus[i].getScore();
}
Arrays.sort(a);//升序排序
for (int i = a.length - 1; i >= 0 ; i--) {
for (int j = 0; j < stus.length; j++) {
if(stus[j].getScore() == a[i]){
System.out.println(stus[j]);
}
}
}
}
public static void main(String[] args) {
stus = new Student[3];
stus[0] = new Student(1,"a",87);
stus[1] = new Student(2,"b",78);
stus[2] = new Student(3,"c",99);
System.out.println("按分数从高到低排序:");
sort(stus);
}
}按分数从高到低排序:
3 c 99
1 a 87
2 b 78
望采纳哦,亲!
追答:刚才你是不是还问了一道一模一样的题?。。。
全部回答
- 1楼网友:佘樂
- 2021-02-19 18:24
import java.util.*;
public class Student {
private String name;
private int no;
private double score;
public Student(String name, int no, double score) {
this.name = name;
this.no = no;
this.score = score;
}
public String toString() {
return String.format("Student[name=%s, no=%d, score=%.2f]", name, no, score);
}
public static void main(String[] args) {
Student s1 = new Student("yugi", 1, 100);
Student s2 = new Student("kaiba", 2, 90);
Student s3 = new Student("joey", 3, 98);
Student[] arr = {
s1, s2, s3
};
System.out.println("排序前:" + Arrays.toString(arr));
Arrays.sort(arr, new Comparator < Student > () {
public int compare(Student s1, Student s2) {
if (s1.score > s2.score) {
return -1;
} else if (s1.score < s2.score) {
return 1;
} else {
return 0;
}
}
});
System.out.println("排序后:" + Arrays.toString(arr));
}
}
public class Student {
private String name;
private int no;
private double score;
public Student(String name, int no, double score) {
this.name = name;
this.no = no;
this.score = score;
}
public String toString() {
return String.format("Student[name=%s, no=%d, score=%.2f]", name, no, score);
}
public static void main(String[] args) {
Student s1 = new Student("yugi", 1, 100);
Student s2 = new Student("kaiba", 2, 90);
Student s3 = new Student("joey", 3, 98);
Student[] arr = {
s1, s2, s3
};
System.out.println("排序前:" + Arrays.toString(arr));
Arrays.sort(arr, new Comparator < Student > () {
public int compare(Student s1, Student s2) {
if (s1.score > s2.score) {
return -1;
} else if (s1.score < s2.score) {
return 1;
} else {
return 0;
}
}
});
System.out.println("排序后:" + Arrays.toString(arr));
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯