题目:有一个String类型的变量 str="login",要求输出他的所有可能的任意大小写组合的结果,如:“Login,LOgin,LoGiN......”请回复整个程序代码,用System.out.print语句打印结果有不明白的地方,可以跟帖追问,:)

解决方案 »

  1.   

    就是个二进制。0代表小写,1代表大写login供5个字母,你就从 00000 遍历到 11111 ,完成任务。总计 0 ~ 31 供32种组合。
      

  2.   

    for examplepublic class Test {
        public static void main(String[] args) throws Throwable {
            String s = "login".toLowerCase();
            char[] c = s.toCharArray();
            int[] idx = new int[c.length];
            StringBuilder buf = new StringBuilder();
            while (idx[0] < 2) {
                buf.delete(0, buf.length());
                for (int i=0; i<idx.length; i++) {
                    if (idx[i] == 1) {
                        buf.append(Character.toUpperCase(c[i]));
                    } else {
                        buf.append(c[i]);
                    }
                }
                System.out.println(buf);            idx[idx.length-1]++;
                for (int i=idx.length-1; i>0; i--) {
                    if (idx[i] == 2) {
                        idx[i] = 0;
                        idx[i-1]++;
                    } else {
                        break;
                    }
                }
            }
        }
    }
      

  3.   

    4楼的好像没考虑Olgin,Golin这样组合的问题吧
      

  4.   


    public class Test26 { public static void main(String[] args) {
    String str = "lOgiN";
    String lowStr = str.toLowerCase();
    int length  = lowStr.length();
    for(int i=0; i<(int)Math.pow(2, lowStr.length());i++){
    char[] ch = lowStr.toCharArray();
    String s = Integer.toBinaryString(i);
    for(int j=0; j<s.length(); j++){
    if('1' == s.charAt(s.length() -j -1))
    {
    ch[length - j -1] = (char)(ch[length - j -1] - 32);
    }
    }
    System.out.println(new String(ch));
    }
    }
    }