java 提问帮忙一下
设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。
[解决办法]
package qhxz0405;
/*5.设计一个类Student,该类包括姓名、学号和成绩。
* 设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。
* */
public class Student {
String name;
int number;
double grade;
Student( String name,int number,double grade){
this.name=name;
this.number=number;
this.grade=grade;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
void show(){
System.out.println( "姓名: "+name+ "\t学号: "+number+ "\t成绩: "+grade);
}
}
package qhxz0405;
public class Demo {
public static void main(String[] args) {
Student t;
Student[] a = { new Student( "张三 ", 1101, 89),
new Student( "李四 ", 1102, 91), new Student( "王二 ", 1103, 99),
new Student( "孙一 ", 1104, 100) };
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
if (a[i].grade < a[j].grade) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
for (int i = 0; i < a.length; i++) {
a[i].show();
}
}
}