让用户输入一串名字(英文)用空格隔开并且首字母大写,然后按照开头字母顺序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] + " ");
}
}
}
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] + " ");
}
}
}
温馨提示:答案为网友推荐,仅供参考