用Java语言写一个成绩排名
时间: 2024-05-11 15:16:07 浏览: 9
的程序,可以实现输入学生姓名及成绩,然后按照成绩从高到低进行排名。程序输出排名、姓名和成绩。
以下是实现该程序的Java代码:
```java
import java.util.*;
public class ScoreRanking {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入学生人数:");
int n = sc.nextInt();
String[] name = new String[n];
int[] score = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的姓名和成绩(用空格隔开):");
name[i] = sc.next();
score[i] = sc.nextInt();
}
// 冒泡排序进行成绩排名
for (int i = 0; i < score.length - 1; i++) {
for (int j = i + 1; j < score.length; j++) {
if (score[i] < score[j]) {
int temp1 = score[i];
score[i] = score[j];
score[j] = temp1;
String temp2 = name[i];
name[i] = name[j];
name[j] = temp2;
}
}
}
// 输出排名、姓名和成绩
System.out.println("成绩排名如下:");
for (int i = 0; i < n; i++) {
System.out.println("第" + (i + 1) + "名:" + name[i] + " " + score[i]);
}
}
}
```