(2)编写一个程序,提示用户输入学生数量、各自的姓名和他们的成绩,并按照成绩 的降序来打印学生的姓名。用java编写
时间: 2024-05-22 14:12:06 浏览: 32
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生数量:");
int n = scanner.nextInt();
String[] names = new String[n];
int[] scores = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的姓名:");
names[i] = scanner.next();
System.out.print("请输入该学生的成绩:");
scores[i] = scanner.nextInt();
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (scores[i] < scores[j]) {
int tempScore = scores[i];
scores[i] = scores[j];
scores[j] = tempScore;
String tempName = names[i];
names[i] = names[j];
names[j] = tempName;
}
}
}
System.out.println("按成绩降序打印学生姓名:");
for (int i = 0; i < n; i++) {
System.out.println(names[i]);
}
}
}
阅读全文