让puzzel 8项目变得更好的最佳方法是什么?

时间:2017-01-20 13:39:44

标签: vb.net

我有一个puzzle8项目的代码:

Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
        Dim indexrand As Integer
        Dim pos As Integer
        Dim m As Integer
        Dim j As Integer
        For j = 0 To 100
            pos = 0
            indexrand = R.Next(0, 9)
            For i = indexrand To 8
                array.Insert(0, array(i + pos))
                pos += 1
            Next
            m = 9 - indexrand
            Do While m > 0
                array.RemoveAt(array.Count - 1)
                m -= 1
            Loop
        Next

我制作了一个循环播放它们,它完美地工作。然后我希望它给每个按钮一个值,所以我这样做了:

Button1.Text = array(0)
Button2.Text = array(1)
Button3.Text = array(2)
Button4.Text = array(3)
Button5.Text = array(4)
Button6.Text = array(5)
Button7.Text = array(6)
Button8.Text = array(7)
Button9.Text = array(8)`

但是,我在这里遇到问题,因为值9不会变成“”,所以我这样做了:

If Button1.Text = 9 Then
    Button1.Text = ""

ElseIf Button2.Text = 9 Then
    Button2.Text = ""

ElseIf Button3.Text = 9 Then
    Button3.Text = ""

ElseIf Button4.Text = 9 Then
    Button4.Text = ""

ElseIf Button5.Text = 9 Then
    Button5.Text = ""

ElseIf Button6.Text = 9 Then
    Button6.Text = ""

ElseIf Button7.Text = 9 Then
    Button7.Text = ""

ElseIf Button8.Text = 9 Then
    Button8.Text = ""

ElseIf Button9.Text = 9 Then
    Button9.Text = ""
End If `

这确实有效。但是,我的问题是它有很多If语句。有没有办法循环这个而不输入所有这些ifs?

2 个答案:

答案 0 :(得分:0)

此代码可以使用:

Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
    For Each ctrl As Control In Controls
        ' Check if the control is a button and not the Button10
        If TypeOf ctrl Is Button And ctrl.Name <> "Button10" Then
            ' Check if the current button's text is 9                
            If ctrl.Text = "9" Then
                ctrl.Text = ""
            End If
        End If
    Next
End Sub

顺便说一下,我不确定条件If ctrl.Text = "9"是最好的方法。

答案 1 :(得分:0)

首先,启用Option Strict !完成后,试试这个:

Dim Buttons() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9}
For i As Integer = 0 To array.Length - 1
    Buttons(i).Text = If(array(i) = 9," ",array(i).ToString())
Next

这取代了问题中的第2和第3个代码片段。我们可以更有趣地包含第一个代码段:

Public Function Shuffle(Of T)(ByVal items As IList(Of T)) As IList(Of T)
    Static rand As New Random()
    For i As Integer = items.Count - 1 To 1 Step -1
        Dim j As Integer = rand.Next(i + 1)
        Dim temp As T= items(i)
        items(i) = items(j)
        items(j) = temp
    Next
    Return items
End Function

Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click 
    Dim Buttons() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9}
    Dim values = Shuffle(Enumerable.Range(0, 9).ToList())
    For i As Integer = 0 To values.Length - 1
        Buttons(i).Text = If(values(i) = 9," ",values(i).ToString())
    Next
End Sub