public class test2 {
/** test 
 * 
 */
public static void main(String args[]) {
String str ="asfdf123BNHNN@#%^$";
System.out.println("**=" +str);
String re1 ="[a-z]";
String re2 = "[A-Z]";

// 1
StringBuffer sb = new StringBuffer();
for(int i=0; i< str.length(); i++){
String strTmp = "" + str.charAt(i);
if(strTmp.matches( re1)) {
strTmp = strTmp.toUpperCase();
} else if(strTmp.matches( re2)){
strTmp = strTmp.toLowerCase();
}
sb.append(strTmp);
}
System.out.println(sb.toString());

// 2
sb = new StringBuffer();
for(int i=0; i< str.length(); i++){
int intAC2 = (int)str.charAt(i);
String strTmp = "" + str.charAt(i);
if(intAC2>64 && intAC2 < 91) { // A-Z
strTmp = strTmp.toLowerCase();
} else if(intAC2>96 && intAC2 < 123){ // a-z
strTmp = strTmp.toUpperCase();
}
sb.append(strTmp);
}
System.out.println(sb.toString());
}
}

解决方案 »

  1.   

    public class Test {
    public static void main(String args[]) {
    String str =javax.swing.JOptionPane.showInputDialog("请输入一个字符串:");
    System.out.println("你输入的是:" +str);
    String re1 ="[a-z]";
    String re2 = "[A-Z]";
    StringBuffer sb = new StringBuffer();
    for(int i=0; i< str.length(); i++){
    String strTmp = "" + str.charAt(i);
    if(strTmp.matches( re1)) {
    strTmp = strTmp.toUpperCase();
    } else if(strTmp.matches( re2)){
    strTmp = strTmp.toLowerCase();
    }
    sb.append(strTmp);
    }
    System.out.println("转换为:"+sb.toString());
    sb = new StringBuffer();
    for(int i=0; i< str.length(); i++){
    int intAC2 = (int)str.charAt(i);
    String strTmp = "" + str.charAt(i);
    if(intAC2>64 && intAC2 < 91) {
    strTmp = strTmp.toLowerCase();
    } else if(intAC2>96 && intAC2 < 123){
    strTmp = strTmp.toUpperCase();
    }
    sb.append(strTmp);
    }
    }
    }
    记得给分啊!