2015-02-06 40 views
-2

好吧,所以目前我有這段代碼,我試着把ss放在引號出現的地方,它說「我要添加SS的位置」,但它沒有工作,所以我看着這個問題,沒有幫助,我來找你們。我搞砸了我的最後一個問題,所以我希望我做些什麼以及這一個,但反正這裏是基於我用我當前的代碼Eric Lippert's blog post將一維數組的字符串轉換爲文本

Public Class Form1 
Private Function CartesianProduct(Of T)(ParamArray sequences As T()()) As T()() 

    ' base case: 
    Dim result As IEnumerable(Of T()) = {New T() {}} 
    For Each sequence In sequences 
     Dim s = sequence 
     ' don't close over the loop variable 
     ' recursive case: use SelectMany to build the new product out of the old one 
     result = From seq In result 
       From item In s 
       Select seq.Concat({item}).ToArray() 
    Next 
    Return result.ToArray() 
End Function 
Dim s1 As String() = New 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"} 
Dim s2 As String() = New String() {"1", "2", "3", "4", "5", "6", "7", "8", "9"} 

Dim ss As String()() = CartesianProduct(s1, s1, s2, s2, s2, s2, s1, s1) 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    TextBox1.Text = TextBox1.Text + "000" + "Where I want SS to be added to" 
End Sub 

末級

+0

您如何期待它在輸出中被格式化? – 2015-02-06 22:20:27

+0

就像文字一樣,如果你看看ss是如何製作的,我希望它與上面使用的順序一起出來,所以一個例子是xx1234xx,我希望它添加到000以前的列表框,所以我可以使用Notepad ++把它製成列表形式 – ImTheTopOfKEK 2015-02-06 22:25:16

+0

我注意到那些是單字符字符串。如果將這些字符定義爲字符,而不是字符串,則實際上可以極大地提高效率。 – 2015-02-07 05:54:29

回答

0

您可以使用LINQ的的Select方法將二維ss數組轉換爲單維字符串數組。舉例來說,如果你只是想連接所有與他們之間沒有分隔符的第二維弦的,你可以在Select使用String.Join,像這樣:

Dim result As String() = ss.Select(Of String)(Function(x) "000" & String.Join("", x)).ToArray() 

然後你就可以採取1-d字符串數組和使用它的方法AddRange其添加到ListBox,像這樣:

ListBox1.Items.AddRange(ss.Select(Of String)(Function(x) String.Join("", x)).ToArray()) 
+0

所以,如果我這樣做,那麼它會讓我讓列表框/文本框有8使用算法「xx1234xx」的字符串1234 = 1-9 xx = az – ImTheTopOfKEK 2015-02-06 22:48:19

+0

對不起。你失去了我。我不明白你的意思。 – 2015-02-06 22:50:33

+0

好吧我的程序atlteast我試圖做的是使用s1和s2生成8個字符組合的結果我希望能夠通過我的程序的複製粘貼將它們保存到文本文檔中,因此文本框。 – ImTheTopOfKEK 2015-02-06 22:52:09

0
Public Class Form1 
Dim MyMin As Integer = 1 
Dim MyMax As Integer = 9 
Dim My1stRandomNumber As Integer 
Dim My2ndRandomNumber As Integer 


Dim Generator As System.Random = New System.Random() 
Dim prng As New Random 
Const minCH As Integer = 2 
Const maxCH As Integer = 2 

Const randCH As String = "abcdefghijklmnopqrstuvwxyz" 

Private Function RandomString() As String 
    Dim sb As New System.Text.StringBuilder 
    For i As Integer = 1 To prng.Next(minCH, maxCH + 1) 
     sb.Append(randCH.Substring(prng.Next(0, randCH.Length), 1)) 
    Next 
    Return sb.ToString() 
End Function 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Timer1.Enabled = True 

End Sub 

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
    Label1.Text = Generator.Next(MyMin, MyMax + 1) 
    Label2.Text = Generator.Next(MyMin, MyMax + 1) 
    Label3.Text = Generator.Next(MyMin, MyMax + 1) 
    Label4.Text = Generator.Next(MyMin, MyMax + 1) 
    Label5.Text = RandomString() 
    Label6.Text = RandomString() 
    TextBox1.Text = TextBox1.Text + "$" + Label6.Text + Label1.Text + Label2.Text + Label3.Text + Label4.Text + Label5.Text 
End Sub 

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    Timer1.Enabled = False 
End Sub 

末級

是我如何修復它只是使用多個標籤和定時器來切換整數類型觸發循環。

相關問題