本帖最后由 fecuznmn 于 2015-5-31 21:16 编辑
package exam;
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入:");
String str = s.next();
String str1 = trans(str);
System.out.println("大小写转换结果为:" + str1);
s.close();
}
public static String trans(String s) {
char[] ch = s.toCharArray();
for (int x = 0; x < ch.length; x++) {
if (ch[x] >= 'a' && ch[x] <= 'z') {
ch[x] = Character.toUpperCase(ch[x]);
}
if (ch[x] >= 'A' && ch[x] <= 'Z') {
ch[x] = Character.toLowerCase(ch[x]);
}
}
return String.copyValueOf(ch);
}
}
结果是无论输入的是大写还是小写,返回值都是小写。为什么啊? |
|