java从键盘上录入学生人数和每个学生的姓名以及分数,按照分数降序输出,学生名次、学生姓名、学生分数
import java.util.Scanner;/*
* 从键盘上录入学生人数和每个学生的姓名以及分数,按照分数降序输出,学生名次、学生姓名、学生分数
* */
public class Test13 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入学生人数:");int num = sc.nextInt();int[] score = new int[num];String[] names = new String[num];for (int i = 0;i<num;i++){System.out.println("请输入第"+(i+1)+"学生姓名:");names[i] = sc.next();System.out.println("请输入第"+(i+1)+"个学生的分数:");score[i] = sc.nextInt();}for (int i=0;i<num-1;i++){for (int j = 0;j<num-1-i;j++){if (score[j]<score[j+1]){int temp = score[j];score[j] = score[j+1];score[j+1] = temp;//根据分数,置换名字String name = names[j];names[j] = names[j+1];names[j+1] = name;}}}for (int i = 0;i<num;i++){System.out.println("第"+(i+1)+"名\t"+names[i]+"\t"+score[i]+"分");}sc.close();}
}
执行代码如下: