小鑫の日常系列故事(十)——排名次 (java实现)

import java.util.*;
/*next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next()方法会自动将其去掉,
只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。
简单地说,next()查找并返回来自此扫描器的下一个完整标记。完整标记的前后是与分隔模式匹配的输入信息,
所以next方法不能得到带空格的字符串。而nextLine()方法的结束符只是Enter键,即nextLine()方法返回的是Enter键之前的所有字符,
它是可以得到带空格的字符串的。*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String[] name = new String[51];
int[] score = new int[51];
for (int i = 0; i < n; i++) {
name[i] = sc.next(); // nextLine()会吸收回车( )
score[i] = sc.nextInt();
}
for (int i = 0; i < n-1; i++) {
int index = i;
for (int j = i+1; j < n; j++){
if (score[index] > score[j]){
index = j;
}
}
int t = score[index];
score[index] = score[i];
score[i] = t;
String tem = name[index];
name[index] = name[i];
name[i] = tem;
}
for (int i = n-1; i >= 0; i--){
System.out.printf("%s %d ", name[i], score[i]);
}
}
}
原文地址:https://www.cnblogs.com/sugerandmaster/p/11487914.html