2014-10-18 65 views
-1

我想通過給定的字母沒有改變字母的訂單每一個可能的組合會打印,所以我寫了這個代碼,但它會重新打印每行,再有什麼問題組合在java程序冗餘

public class Solutions { 


    public static void main(String[] args) { 
     // Scanner scanner = new Scanner(System.in); 

     c = "l u k".split(" "); 
     Solutions solutions = new Solutions(); 
     solutions.combi(0); 
     System.out.println("Number of combi = " + count); 

     System.out.print(max); 
    } 

    static String[] c; 
    static int count = 0; 
    static int max = 0; 


    public void combi(int start) { 

     int j; 
     if (start != 0) { 
      String str = ""; 
      for (int i = 0; i < start; i++) { 
       // System.out.print(c[i]); 
       str += c[i]; 
      } 


      // System.out.println(); 
      count++; 
     } 

     for (j = start; j < c.length; j++) { 


      combi(start + 1); 


     } 

    } 

} 
+0

如果你使用番石榴,你有工具來促進你的努力 – fge 2014-10-18 15:35:08

+0

嘗試在調試器下運行你的代碼,或者添加打印輸出,以便你可以看到它爲什麼做出什麼決定。觀看它運行。看看它在做什麼意想不到的事情。修理它。重複,直到它工作。 – keshlam 2014-10-18 17:33:09

+0

@keshlam我嘗試它,但意外地啓動變量值更改3到2 – Tikitaka 2014-10-18 17:52:41

回答

0
public void combi(int start) { 
    int j; 
    if (start != 0) { 
     for (int i = 0; i < start; i++) { 
      System.out.print(c[i]); 
     } 

     System.out.println(); 
     count++; 
    } else { 
     for (j = start+1; j <= c.length; j++) { 
      combi(j); 
     } 
    } 
}