Visual Basic如何比较标签中的3个值?

时间:2013-03-20 01:11:20

标签: vb.net

我已尝试过所有可能的组合,但我无法让它正常工作。这个游戏需要测试所有3个骰子是否相同,如果是,那么点值会增加1并显示在lblPoints中。

以下是我需要更改的特定代码行:

    If (lblDice.Text = lblDice2.Text) And (lblDice2.Text = lblDice3.Text) Then
            Points += 1
        End If

如果有帮助,下面是整个游戏:

    Dim randomObject As New Random()
    Dim n, m, o, p As Integer
    Dim RollNumber = 1
    Dim TurnNumber As Integer
    Dim Points As Integer

    Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click
        lblPoints.Text = Points

        If (lblDice.Text = lblDice2.Text) And (lblDice2.Text = lblDice3.Text) Then
            Points += 1
        End If

        If btnHold.Enabled = True Then
            Timer1.Enabled = True
        End If
        If btnHold2.Enabled = True Then
            Timer2.Enabled = True
        End If
        If btnHold3.Enabled = True Then
            Timer3.Enabled = True
        End If

        TurnNumber += 1
        lblTurns.Text = TurnNumber

        If TurnNumber = 3 Then
            RollNumber += 1
            TurnNumber = 0
        End If

        lblRolls.Text = RollNumber

        If RollNumber = 5 Then
            btnRoll.Enabled = False
            If Points = 0 Or 1 Then
                lblPoints2.Text = "You have done poorly"
            ElseIf Points = 2 Or 3 Then
                lblPoints2.Text = "Mediocre at best"
            ElseIf Points = 4 Then
                lblPoints2.Text = "Almost perfect"
            ElseIf Points = 5 Then
                lblPoints2.Text = "Almost perfect"
                picJackpot.Visible = True
            End If
        End If


    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        m = m + 1
        If m < 10 Then
            n = randomObject.Next(1, 7)
            lblDice.Text = n
            die1PictureBox.Image = ImageList1.Images(n - 1)
        Else
            Timer1.Enabled = False
            m = 0
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        m = m + 1
        If m < 10 Then
            n = randomObject.Next(1, 7)
            lblDice2.Text = n
            die2PictureBox.Image = ImageList1.Images(n - 1)
        Else
            Timer2.Enabled = False
            m = 0
        End If
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        m = m + 1
        If m < 10 Then
            n = randomObject.Next(1, 7)
            lblDice3.Text = n
            die3PictureBox.Image = ImageList1.Images(n - 1)
        Else
            Timer3.Enabled = False
            m = 0
        End If
    End Sub

    Private Sub btnHold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHold.Click
        btnHold.Enabled = False
        btnUnhold.Enabled = True
    End Sub

    Private Sub btnUnhold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhold.Click
        btnHold.Enabled = True
        btnUnhold.Enabled = False
    End Sub

    Private Sub btnHold2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHold2.Click
        btnHold2.Enabled = False
        btnUnhold2.Enabled = True
    End Sub

    Private Sub btnUnhold2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhold2.Click
        btnHold2.Enabled = True
        btnUnhold2.Enabled = False
    End Sub

    Private Sub btnHold3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHold3.Click
        btnHold3.Enabled = False
        btnUnhold3.Enabled = True
    End Sub

    Private Sub btnUnhold3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhold3.Click
        btnHold3.Enabled = True
        btnUnhold3.Enabled = False
    End Sub

    Private Sub btnRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestart.Click
        Dim msg As String
        Dim title As String
        Dim style As MsgBoxStyle
        Dim response As MsgBoxResult
        msg = "Are you sure you want to start a new game? If not, you can select 'No' to continue with your current game."
        style = MsgBoxStyle.DefaultButton2 Or _
           MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
        title = "New Game"

        response = MsgBox(msg, style, title)
        If response = MsgBoxResult.Yes Then
            Reset()

        End If



    End Sub


    Private Sub ColorChangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorChangeToolStripMenuItem.Click

        'Notice the ... in the menu...that means a dialog box will come

        Dim dialog As New ColorDialog   ' Color Dialog
        Dim result As DialogResult ' stores Button clicked

        dialog.FullOpen = True ' show all colors
        result = dialog.ShowDialog

        ' do nothing if user clicked dialog's Cancel Button
        If result <> Windows.Forms.DialogResult.Cancel Then
            ' assign new color to Paint object
            Label1.ForeColor = dialog.Color
        End If


    End Sub

    Private Sub FontChangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontChangeToolStripMenuItem.Click
        'Notice the ... in the menu...that means a dialog box will come

        Dim dialog As New FontDialog   ' Font Dialog
        Dim result As DialogResult ' stores Button clicked

        ' show dialog and get result
        result = dialog.ShowDialog()

        ' do nothing if user clicked dialog's Cancel Button
        If result <> Windows.Forms.DialogResult.Cancel Then
            ' assign new font value to TextBox
            If Windows.Forms.DialogResult.OK = MessageBox.Show("Changing Font. Click Ok to Change,\n Cancel for no change to occur.", _
                                     "Warning", MessageBoxButtons.OKCancel) Then
                Label1.Font = dialog.Font
            End If


        End If


    End Sub

    Private Sub SelectAColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAColorToolStripMenuItem.Click

        Dim dialog As New ColorDialog   ' Color Dialog
        Dim result As DialogResult ' stores Button clicked

        dialog.FullOpen = True ' show all colors
        result = dialog.ShowDialog()

        ' do nothing if user clicked dialog's Cancel Button
        If result <> Windows.Forms.DialogResult.Cancel Then
            Me.BackColor = dialog.Color
        End If

    End Sub

    Private Sub SelectAnImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAnImageToolStripMenuItem.Click
        Dim msg As String
        Dim title As String
        Dim style As MsgBoxStyle
        Dim response As MsgBoxResult
        msg = "Are you sure you want to see what happens when you get a perfect score without actually playing? That's cheating.. and will reset the game!"
        style = MsgBoxStyle.DefaultButton2 Or _
           MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
        title = "New Game"

        response = MsgBox(msg, style, title)
        If response = MsgBoxResult.Yes Then
            Dim FileChooser As New OpenFileDialog()
            Dim result As DialogResult

            result = DialogResult = FileChooser.ShowDialog()

            If result <> Windows.Forms.DialogResult.Cancel Then
                If FileChooser.FileName <> "" Then
                    Me.BackgroundImage = System.Drawing.Image.FromFile(FileChooser.FileName)
                    Reset()
                Else
                    MessageBox.Show("No Change Made. File Not Selected!", "Warning", _
                                    System.Windows.Forms.MessageBoxButtons.OK, _
                                    System.Windows.Forms.MessageBoxIcon.Exclamation)
                End If
            End If
        End If

    End Sub

    Private Sub btnHow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHow.Click
        MessageBox.Show(" You will have 5 rolls.  In each roll you have up to 3 turns. Your object in each of the turns is to get three of a kind on the dice.  You can hold some die and roll only some of the die, or you can reroll all the dice.  If in a turn you get three of a kind, you get a point.  At the end of 5 turns you could have a maximum of 5 points and you will get a special prize.")
    End Sub

    Sub Reset()
        RollNumber = 1
        lblRolls.Text = RollNumber
        TurnNumber = 0
        lblTurns.Text = TurnNumber
        Points = 0
        lblPoints.Text = Points
        lblPoints2.Text = ""

        btnRoll.Enabled = True
        btnHold.Enabled = True
        btnHold2.Enabled = True
        btnHold3.Enabled = True
        btnUnhold.Enabled = False
        btnUnhold2.Enabled = False
        btnUnhold3.Enabled = False

        die1PictureBox.Image = Nothing
        die2PictureBox.Image = Nothing
        die3PictureBox.Image = Nothing
        lblDice.Text = ""
        lblDice2.Text = ""
        lblDice3.Text = ""
    End Sub
End Class

3 个答案:

答案 0 :(得分:2)

正在发生的事情是,在运行“重置子程序”和启动程序后,您在“”上获得了有效的比较,因为您的骰子被初始化为相同的值。尝试使用布尔值来表示一个新鲜的游戏,当你的骰子从头开始时,你就不会得到匹配。这样的事情。

在声明中添加一个布尔变量

Dim Restart as Boolean = True

在您的BtnRoll_Click EventHandler

If Not restart Then
    If (lblDice.Text = lblDice2.Text) And (lblDice2.Text = lblDice3.Text) Then
        Points += 1
    End If
End If
Restart = False

并将其添加到Reset Subroutine的末尾。

Restart = True

答案 1 :(得分:0)

尝试

If String.Compare(lblDice.Text, lblDice2.Text) = 0 And String.Compare(lblDice2.Text, lblDice3.Text) = 0 Then
    Points += 1
Else

答案 2 :(得分:-1)

tente (val(text.text)) then   


exemplo  if text1 = (val(text2.text)) then
 text1 =0 
end if