题目:有一个String类型的变量 str="login",要求输出他的所有可能的任意大小写组合的结果,如:“Login,LOgin,LoGiN......”
请回复整个程序代码,用System.out.print语句打印结果
有不明白的地方,可以跟帖追问,:)
------解决方案--------------------
for example
- Java code
public 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; } } } }}
------解决方案--------------------
- Java code
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)); } }}