给出n个字符串,将每个字符串的大写字母修改成小写字母,小写字母修改成大写字母,然后按照字典序从小到大输出。Java实现
时间: 2024-05-19 07:16:06 浏览: 54
用Java将字符串的首字母转换大小写
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String[] strs = new String[n];
for (int i = 0; i < n; i++) {
strs[i] = scanner.next();
}
// 将每个字符串的大小写字母互换
for (int i = 0; i < n; i++) {
char[] chs = strs[i].toCharArray();
for (int j = 0; j < chs.length; j++) {
if (Character.isUpperCase(chs[j])) {
chs[j] = Character.toLowerCase(chs[j]);
} else {
chs[j] = Character.toUpperCase(chs[j]);
}
}
strs[i] = new String(chs);
}
// 按字典序从小到大排序
Arrays.sort(strs);
// 输出结果
for (String str : strs) {
System.out.println(str);
}
}
}
阅读全文