1个回答
展开全部
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Student{
private String name;
private int java;
public Student(String name, int java) {
super();
this.name = name;
this.java = java;
}
@Override
public String toString() {
return "Student [name=" + name + ", java=" + java + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getJava() {
return java;
}
public void setJava(int java) {
this.java = java;
}
}
public class Demo{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Student[] stu = new Student[4];
for(int i=0;i<4;i++){
System.out.print("请输入第"+(i+1)+"个人的名字:");
String name=sc.nextLine();
System.out.print("请输入第"+(i+1)+"个人的java成绩:");
String java=sc.nextLine();
stu[i]=new Student(name,Integer.parseInt(java));
}
Arrays.sort(stu,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.getJava()-o2.getJava();
};
});
for (Student student : stu) {
System.out.println(student);
}
}
}
import java.util.Comparator;
import java.util.Scanner;
class Student{
private String name;
private int java;
public Student(String name, int java) {
super();
this.name = name;
this.java = java;
}
@Override
public String toString() {
return "Student [name=" + name + ", java=" + java + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getJava() {
return java;
}
public void setJava(int java) {
this.java = java;
}
}
public class Demo{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Student[] stu = new Student[4];
for(int i=0;i<4;i++){
System.out.print("请输入第"+(i+1)+"个人的名字:");
String name=sc.nextLine();
System.out.print("请输入第"+(i+1)+"个人的java成绩:");
String java=sc.nextLine();
stu[i]=new Student(name,Integer.parseInt(java));
}
Arrays.sort(stu,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.getJava()-o2.getJava();
};
});
for (Student student : stu) {
System.out.println(student);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询