2011-02-18 76 views
0

是否有任何簡單的方法來創建未知深度大小的循環,所以我們不必創建此循環,但我們可以使用一個函數。JavaFX未知循環深度

var letters = [97..122]; 

//size 1 
for (x in letters) print(x as Character); 

//size 2 
for (x in letters, y in letters) { 
     print(x as Character); 
     println(y as Character); 
    } 

//size 20 
for (x1 in letters, .., x20 in letters) { 
     print(..); 
    } 

在輸出有: AA AB 交流

等。

我知道我必須以這種方式來回復。

謝謝。

回答

0
public static void printAlphabetCombinations(int recurseCounter, String current) { 
    String temp = current; 
    if(recurseCounter == 0) { 
     System.out.println(temp); 
     numberOfCombinations ++; 
     return; 
    } 
    else { 
     recurseCounter--; 
     for(int i = char_start; i < char_end+1; i++) { 
     temp = current + (char) (i); 
     printAlphabetCombinations(recurseCounter, temp); 

     } 
    } 

用法:

printAlphabetCombinations(3, ""); 

感謝所有幫助:)