gpt4 book ai didi

vb.net - 如何创建未定义的循环计数

转载 作者:行者123 更新时间:2023-11-30 23:49:37 24 4
gpt4 key购买 nike

我正在尝试创建一个程序,它将循环遍历所有字母。

我想例如显示 aaaa,然后 aaab 到 aaaz,然后 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 的深度(意思是所有三个字母的排列,正如你在你的问题中所描述的那样)。您可以将值更改为您希望的任何值,或者更好的是读取用户的输入。

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

关于vb.net - 如何创建未定义的循环计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6128790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com