java编写一个程序,用数组存储5位学生的姓名和成绩,姓名使用String类型,成绩用int类型,按照成绩的降序对数组排序,在对成绩排序时要保证姓名与成绩的对应关系不改变。 请按成绩降序打印输出学生的姓名和成绩。
时间: 2024-05-02 13:17:49 浏览: 112
```
import java.util.Arrays;
public class StudentSort {
public static void main(String[] args) {
String[] names = {"Tom", "Jerry", "Alice", "Bob", "Tim"};
int[] scores = {80, 90, 70, 85, 95};
//按成绩降序排序
for(int i = 0; i < scores.length - 1; i++){
for(int j = i + 1; j < scores.length; 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;
}
}
}
//打印输出
for(int i = 0; i < scores.length; i++){
System.out.println(names[i] + " " + scores[i]);
}
}
}
```
阅读全文
相关推荐



















