2011-05-25 21 views
4

我試圖創建一個程序,它將循環槽所有字母。如何創建未定義的循環計數

我想例如顯示AAAA,然後AAABaaaz,然後AABA等方面ZZZZ

問題是:如何讓用戶輸入字母數?

這裏是我的代碼只有3個字母:

Dim abc() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", _ 
    "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} 
Console.ReadLine() 
Dim indx As Integer = 0 
For a = 0 To 25 
    For b = 0 To 25 
     For c = 0 To 25 
       Console.WriteLine("{0}{1}{2}", abc(a), abc(b), abc(c))   
     Next 
    Next 
Next 

回答

3

遞歸的解決方案是你正在尋找這裏的工具。你想重複一定的深度;該深度是來自用戶的輸入。在這個例子中,我提供了3的深度(這意味着所有三個字母的排列,就像你在你的問題中描述的那樣)。您可以根據自己的意願更改值,或者更好地閱讀用戶的輸入內容。

Dim abc() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} 

Sub Main() 
    Dim depth As Integer 
    depth = 3 
    IterateAlphabet("", depth) 
    Console.ReadKey() 

End Sub 

Sub IterateAlphabet(ByVal currentLetters As String, ByVal currentDepth As Integer) 
    For letter = 0 To 25 
     Dim newLetters As String 
     newLetters = currentLetters + abc(letter) 
     If (currentDepth = 1) Then 
      Console.WriteLine("{0}", newLetters) 
     Else 
      IterateAlphabet(newLetters, currentDepth - 1) 
     End If 
    Next 

End Sub 
0

你可以看看使用的輸入框。允許用戶選擇長度。 MSDN InputBox

Dim Length as Object 
Message = "Hello" 
Title = "Hello" 
InputBox(Message, Title, Length) 

Console.WriteLine(abc(Length)) 

這是把我的頭頂部,但我相信有一些調整它應該做的伎倆

0

很抱歉,但我不堅定VB。這是一個訣竅的Python代碼片段。也許你可以把它作爲僞代碼並將其轉換爲VB。

任何人都可以將其更改爲VB,請編輯我的帖子。

strlen是輸出字符串的長度。 a ** b是b的力量。 a%b是模b。

strlen = 5 

for i in range (26 ** strlen): 
    output = '' 
    for j in range (strlen): 
     output = chr ((i/(26 ** j)) % 26 + 97) + output 
    print output 

並且不要過度使用輸出長度。例如10的長度可以產生141167095653376個字符串。

2

我建議遞歸,我不是在我的VB技巧沒有信心,但這裏是C#

static String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
           "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; 

    static void Main(string[] args) 
    { 
     Console.Writeline("Enter the amount of characters"); 
     int count = Int32.Parse(Console.ReadLine()); 
     outputStrings("", count); 
     Console.ReadLine(); 
    } 

    static public void outputStrings(String startString, int letterCount) 
    { 
     for (int i = 0; i < letters.Length; i++) 
     { 
      String temp = startString; 
      temp += letters[i]; 

      if (temp.Length == letterCount) 
      { 
       Console.WriteLine(temp); 
      } 
      else 
      { 
       outputStrings(temp, letterCount); 
      } 
     } 
    }