Java中让用户随机输入一串名字并按照字母顺序输出

让用户输入一串名字(英文)用空格隔开并且首字母大写,然后按照开头字母顺序output
名字的数量是随机的
求大神

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一组名字: ");
String s = sc.nextLine();
String[] strs = s.split(" ");
String temp = "";
for(int i = 0; i < strs.length; i++){
for(int j = i; j > 0; j--){
if (strs[j].charAt(0) < strs[j - 1].charAt(0)) {
temp = strs[j - 1];
strs[j - 1] = strs[j];
strs[j] = temp;
} else
break;
}
}
for(int i = 0; i < strs.length; i++){
System.out.print(strs[i] + " ");
}
}
}

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网