public class cumt { int [] array = new int[]{1,2,3,4,5,6,7,8,9,0}; int hope = 15; public void action(int index,int hope,int[] array,String tai){ String xyz=""; for (int i = index; i < array.length; i++) { int j = array[i]; xyz = tai + " + " + j; if(j<hope){ action(i+1,hope-j,array,xyz); } if(j==hope){ System.out.println(xyz.substring(3)+" ="+this.hope); tai = ""; } } } public static void main(String[] args) { cumt cumt = new cumt(); cumt.action(0, cumt.hope, cumt.array, ""); } } 运行结果如下: 1 + 2 + 3 + 4 + 5 =15 1 + 2 + 3 + 9 =15 1 + 2 + 4 + 8 =15 1 + 2 + 5 + 7 =15 1 + 3 + 4 + 7 =15 1 + 3 + 5 + 6 =15 1 + 5 + 9 =15 1 + 6 + 8 =15 2 + 3 + 4 + 6 =15 2 + 4 + 9 =15 2 + 5 + 8 =15 2 + 6 + 7 =15 3 + 4 + 8 =15 3 + 5 + 7 =15 4 + 5 + 6 =15 6 + 9 =15 7 + 8 =15