编写一个程序,提示用户输入学生个数,学生姓名和他们的成绩,然后按照学生成绩的降序打印学生成绩的排行榜.

使用java编写

import java.util.Arrays;
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("请输入学生个数");
String strStuNum = reader.nextLine();
int stuNum = Integer.parseInt(strStuNum);
String[] sNames = new String[stuNum];
int[] sScore = new int[stuNum];
for(int i = 0;i < stuNum; i++){
System.out.println("请输入第"+(i+1)+"个学生的姓名");
sNames[i] = reader.nextLine();
System.out.println("请输入"+sNames[i]+"的分数");
sScore[i] = Integer.parseInt(reader.nextLine());
}
int tempScore;
String tempName;
for (int i = stuNum - 1; i > 0; --i) {
boolean isSort=false;
for (int j = 0; j < i; ++j) {
if (sScore[j + 1] < sScore[j]) {
tempScore = sScore[j];
tempName = sNames[j];
sScore[j] = sScore[j + 1];
sNames[j] = sNames[j + 1];
sScore[j + 1] = tempScore;
sNames[j + 1] = tempName;
isSort=true;
}
}
if(!isSort)break;
}
for(int i = stuNum - 1; i >= 0;i--){
System.out.println("第"+(stuNum - i)+"名\t"+sNames[i]+"\t"+sScore[i]);
}
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答