java创建学生类,包含学号,姓名,成绩等成员变量及其对应方法,通过键盘输入若干个学生的信息,直
展开全部
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
class Student implements Comparable<Student>{
private String xh; //学号
private String xm; //姓名
private double cj; //成绩
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public double getCj() {
return cj;
}
public void setCj(double cj) {
this.cj = cj;
}
public int compareTo(Student other) {
if (cj<other.cj) //这里比较的是什么 sort方法实现的就是按照此比较的东西从小到大排列
return -1 ;
if (cj>other.cj)
return 1 ;
return 0 ;
}
}
public class StudentComp{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Student> list = new ArrayList<Student>();
byte b = 57;
String isjs = "";
int i = 1;
while(!"#".equals(isjs)){
Student stu = new Student();
System.out.println("请输入第"+(i++)+"个学生信息:");
System.out.print("学号:");
stu.setXh(scanner.next());
System.out.print("姓名:");
stu.setXm(scanner.next());
System.out.print("成绩:");
stu.setCj(scanner.nextDouble());
list.add(stu);
System.out.print("是否结束?(输入“#”结束,输入其他任意字符继续)");
isjs = scanner.next();
}
Student[] students = (Student[])list.toArray(new Student[list.size()]);
// for(int j=0;j<list.size();j++){
// students[j] = list.get(j);
// }
Arrays.sort(students);
for(Student s:students){
System.out.println("学号:"+s.getXh()+" 姓名:"+s.getXm()+" 成绩:"+s.getCj());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询