java数组:输入班级人数,然后循环输入学生的名字和该学生的成绩,然后进行排序.
1个回答
展开全部
import java.util.Scanner;
public class Student {
String name;
double score;
public static void main(String[] args) {
System.out.println("请输入班级学生人数");
Scanner sc = new Scanner(System.in);
int totalStudent = sc.nextInt();
Student[] stus = new Student[totalStudent];
for(int i=0;i<totalStudent;i++){
Student s = new Student();
stus[i]=s;
System.out.println("请输入第"+(i+1)+"个学生的名字");
s.name = sc.next();
System.out.println("请输入第"+(i+1)+"个学生的分数");
s.score = sc.nextDouble();
}
bubbleSort(stus);
printStudents(stus);
sc.close();
}
public static void bubbleSort(Student[] s){
for(int i=0;i<s.length-1;i++){
for(int j=i;j<s.length;j++){
if(s[i].score<s[j].score){
Student temp = s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
}
public static void printStudents(Student[] s){
System.out.println("名次\t名字\t成绩");
for(int i=0;i<s.length;i++){
System.out.println(i+1+"\t"+s[i].name+"\t"+s[i].score);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询