package exam.test;import java.util.ArrayList;import java.util.Scanner;public class StringToUper {// 得到一个数组中不同的组合形式--从min个到max个// 虽然本例没有使用,但在其他应用中还是可以用到的public ArrayList<Object[]> getZhFromAry(Object[] ary, int max, int min)throws Exception {int len = ary.length;if (max > len || min >= max) {throw new Exception("最长组合数量不应超过数组长度,并且应大于最短组合数量");}ArrayList<Object[]> lst = new ArrayList<Object[]>();int size;Object[] temp;for (int i = min; i <= max; i++) {int[][] results = getZH(len, i);size = results.length;for (int j = 0; j < size; j++) {temp = new Object[i];for (int k = 0; k < i; k++) {temp[k] = ary[results[j][k]];}lst.add(temp);}}return lst;}// len: 从顺序的几个数中取,顺序从0开始,如len为8,表示从0~7这8个数字中取// qty: 组合的数字数量,如 4 ,表示从上述数组中取不同的4数组合// 返回:所有的组合public int[][] getZH(int len, int qty) throws Exception {int[][] results = null;if (len < qty) {throw new Exception("无法从 " + len + " 个数中取 " + qty + "数组合!");}if (qty == 1) {results = new int[len][1];for (int i = 0; i < len; i++) {results[i][0] = i;}return results;} else if (len == qty) {results = new int[1][qty];for (int i = 0; i < qty; i++) {results[0][i] = i;}return results;}// 定义返回数组的一维长度int top = 1;for (int i = len; i >= len - qty + 1; i--) {top *= i;}int bottom = 1;for (int i = qty; i >= 2; i--) {bottom *= i;}int size = top / bottom;results = new int[size][qty];// 初始化 前qty-1个固定数数组int[] ary1 = new int[qty - 1];for (int i = 0; i < qty - 1; i++) {ary1[i] = i;}// 调用opeZH填充返回数组opeZH(ary1, results, len, qty, 0, size);return results;}// 根据前qty-1个固定数数组,循环确定最后一个数,把这些数组加入大数组// 然后改变固定数数组的某个数及其后续数// 不断进行递归调用private void opeZH(int[] ary1, int[][] results, int len, int qty, int ix,int size) {// System.out.println("opeZH--" + ix);for (int i = ary1[qty - 2] + 1; i < len; i++) {int[] ary2 = new int[qty];for (int j = 0; j < qty - 1; j++) { // 前qty-1个数与固定数数组一样ary2[j] = ary1[j];}ary2[qty - 1] = i; // 第qty个数为循环数results[ix] = ary2;ix++;}// 从后向前确定哪个数要递增,该数递增后,其后的数都要改变for (int i = qty - 2; i >= 0; i--) {if (ary1[i] < i + len - qty) {ary1[i]++;for (int j = i + 1; j < qty - 1; j++) {ary1[j] = ary1[j - 1] + 1;}break;}}// 如果还没有完成所有的变化,则继续递归调用自身if (ary1[0] < 0 + len - qty) {opeZH(ary1, results, len, qty, ix, size);} else { // 最后一种变化int[] ary2 = new int[qty];for (int i = 0; i < qty; i++) {ary2[i] = i + len - qty;}results[ix] = ary2;}}public static void main(String[] args) throws Exception {StringToUper zh = new StringToUper();/* * //测试组合--由此可以看出组合的规律,从后往前看 int len = 7; int qty = 1; int[][] results = * zh.getZH(len,qty); int size = results.length; for(int i=0;i<size;i++) * { for(int j=0;j<qty;j++) { System.out.print(results[i][j]+"/t"); } * System.out.println(); } *//* * //测试一个数组中 n~m 个不同的组合形式 String[] ss = {"a","b","c","1","2","3"}; * ArrayList l = zh.getZhFromAry(ss,5,3); //3个到5个的组合形式 Iterator it = * l.iterator(); Object[] temp; int len; while(it.hasNext()) { temp = * (Object[])it.next(); len = temp.length; for(int i=0;i<len;i++) { * System.out.print(temp[i]); } System.out.println(); } */// 根据字符串的最初形态,依次将其中1~n个字符转大写,将转换后的字符串加入集合Scanner sc=new Scanner(System.in) ;System.out.println("请输入一组小写字母组成的字符串");String str = sc.next();int len = str.length();int size;String temp;char[] chars = null;ArrayList<String> lst = new ArrayList<String>();lst.add(str);for (int i = 1; i <= len; i++) { // 要转换的字符数从1个字符到len个字符int[][] results = zh.getZH(len, i); // 得到所有要转换的字符的下标size = results.length;for (int j = 0; j < size; j++) {chars = str.toCharArray();for (int k = 0; k < i; k++) {chars[results[j][k]] = (char) (chars[results[j][k]] - 32);}temp = new String(chars);lst.add(temp);}}System.out.println("您要的结果:"+lst);}} 追问 非常谢谢
把字符串变成大小写的组合但位置不变的程序如下:import java.util.Scanner;public class FFF { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入字符串:"); String s=sc.next(); int num=s.length(); int n=(int)Math.pow(2,num); char ch; for(int i=0;i<n;i++){ String tmp=Integer.toBinaryString(i); while(tmp.length()<s.length()){ tmp="0"+tmp; } String result=""; for(int j=0;j<tmp.length();j++){ char c=tmp.charAt(j); if(c=='1'){ ch=(char)((int)s.charAt(j)-32); }else{ ch=s.charAt(j); } result=result+ch; } System.out.println(result); } }}运行结果:请输入字符串:abcabcabCaBcaBCAbcAbCABcABC