程序错误(产生错误答案)

时间:2014-10-15 00:03:22

标签: vb.net-2010

我正在为我的编程类制作一个二十一点程序(仅使用1到10之间的数字,不能使用全局变量,这个原因超出了我的意义)。无论何时,我按下但是按下(按钮命中),它总能得到答案,"胸围:领带"即使计算机和你自己的手的数值都明显低于21,这种情况也会发生。如果你按下但是怎么办。

lblPlayV检查玩家是否按下了ButPlay以获取随机值等。

lblCounter表示已经按下了多少次。

我不知道会出现什么问题。

以下是代码:

Public Class Form1

Private Sub butClose_Click(sender As System.Object, e As System.EventArgs) Handles butClose.Click
    End
End Sub

Private Sub butPlay_Click(sender As System.Object, e As System.EventArgs) Handles butPlay.Click
    Dim rand As New Random

    Dim intM1 As Integer = rand.Next(1, 11)
    Dim intM2 As Integer = rand.Next(1, 11)
    Dim intM3 As Integer = rand.Next(1, 11)
    Dim intM4 As Integer = rand.Next(1, 11)
    Dim intM5 As Integer = rand.Next(1, 11)

    Dim intOP1 As Integer = rand.Next(1, 11)
    Dim intOP2 As Integer = rand.Next(1, 11)
    Dim intOP3 As Integer = rand.Next(1, 11)

    lblPlayV.Text = 1
    lblCounter.Text = 1
    butPlay.Text = "Replay"

    lblM1.Text = intM1
    lblM2.Text = intM2
    lblM3.Text = intM3
    lblM4.Text = intM4
    lblM5.Text = intM5

    lblOP1.Text = intOP1
    lblOP2.Text = intOP2
    lblOP3.Text = intOP3

    lblM1.Visible = True
    lblM2.Visible = True
End Sub

Private Sub butHit_Click(sender As System.Object, e As System.EventArgs) Handles butHit.Click
    If lblPlayV.Text = 1 Then
        '<<<<<<<<<<<<<<<<<<<<<<'
        If lblCounter.Text = 1 Then
            lblM3.Visible = True

            If lblM1.Text + lblM2.Text + lblM3.Text = 21 Then
                If lblOP1.Text + lblOP2.Text + lblOP3.Text = 21 Then
                    MsgBox("21 : Draw")
                Else
                    MsgBox("21 : You Win")
                End If

            ElseIf lblM1.Text + lblM2.Text + lblM3.Text > 21 Then
                If lblOP1.Text + lblOP2.Text + lblOP3.Text > 21 Then
                    MsgBox("Bust : Draw")
                Else
                    MsgBox("Bust : You Lose")
                End If

            Else
                lblCounter.Text = lblCounter.Text + 1
            End If

        End If

        '<<<<<<<<<<<<<<<<<<<<<<'
        If lblCounter.Text = 2 Then
            lblM4.Visible = True

            If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text = 21 Then
                If lblOP1.Text + lblOP2.Text + lblOP3.Text = 21 Then
                    MsgBox("21 : Draw")
                Else
                    MsgBox("21 : You Win")
                End If

            ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text > 21 Then
                If lblOP1.Text + lblOP2.Text + lblOP3.Text > 21 Then
                    MsgBox("Bust : Draw")
                Else
                    MsgBox("Bust : You Lose")
                End If

            Else
                lblCounter.Text = lblCounter.Text + 1
            End If

        End If

        '<<<<<<<<<<<<<<<<<<<<<<'
        If lblCounter.Text = 3 Then
            lblM5.Visible = True

            If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text = 21 Then
                If lblOP1.Text + lblOP2.Text + lblOP3.Text = 21 Then
                    MsgBox("21 : Draw")
                Else
                    MsgBox("21 : You Win")
                End If

            ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text > 21 Then
                If lblOP1.Text + lblOP2.Text + lblOP3.Text > 21 Then
                    MsgBox("Bust : Draw")
                Else
                    MsgBox("Bust : You Lose")
                End If

            Else
                If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                    MsgBox("Draw")

                ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                    MsgBox("You Win")

                Else
                    MsgBox("You Lose")
                End If
            End If
            lblCounter.Text = 0
            lblPlayV.Text = 0
        End If

        '<<<<<<<<<<<<<<<<<<<<<<'
    End If
End Sub

Private Sub ButStand_Click(sender As System.Object, e As System.EventArgs) Handles ButStand.Click
    If lblPlayV.Text = 1 Then
        lblPlayV.Text = 0

        If lblM3.Visible = False Then
            If lblM1.Text + lblM2.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                MsgBox("Tie")

            ElseIf lblM1.Text + lblM2.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                MsgBox("You Win")
            Else
                MsgBox("You Lose")
            End If
        End If

        If lblCounter.Text = 1 Then
            If lblM1.Text + lblM2.Text + lblM3.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                MsgBox("Tie")

            ElseIf lblM1.Text + lblM2.Text + lblM3.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                MsgBox("You Win")
            Else
                MsgBox("You Lose")
            End If
        End If

        If lblCounter.Text = 2 Then
            If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                MsgBox("Tie")

            ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                MsgBox("You Win")
            Else
                MsgBox("You Lose")
            End If
        End If

        If lblCounter.Text = 3 Then
            If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                MsgBox("Tie")

            ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
                MsgBox("You Win")
            Else
                MsgBox("You Lose")
            End If
        End If

    End If
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

您的代码中仍然缺少分配....

就像1010所说,如果你得到3张牌“10 10 1”你计算的结果将是“10101”而不是21。

解决问题...“也许你已经解决了问题。”

使用你的整数计算“intM1 + intM2 + intM3”

Dim intM1 As Integer
Dim intM2 As Integer
Dim intM3 As Integer
Dim intM4 As Integer
Dim intM5 As Integer

Dim intOP1 As Integer
Dim intOP2 As Integer
Dim intOP3 As Integer

将它们放在播放按钮之外,以便您可以在表格中的任何位置使用该值 然后在你的播放按钮内给你一个值。

intM1 = rand.Next(1, 11)
intM2 = rand.Next(1, 11)
intM3 = rand.Next(1, 11)
intM4 = rand.Next(1, 11)
intM5 = rand.Next(1, 11)

intOP1 = rand.Next(1, 11)
intOP2 = rand.Next(1, 11)
intOP3 = rand.Next(1, 11)

同样在你的点击按钮中,你需要在活动的底部加上“lblCounter.Text = lblCounter.Text + 1”。

我已经改变了你的一些代码,所以在这里你已经拥有了它。

Dim rand As New Random

Dim intM1 As Integer
Dim intM2 As Integer
Dim intM3 As Integer
Dim intM4 As Integer
Dim intM5 As Integer

Dim intOP1 As Integer
Dim intOP2 As Integer
Dim intOP3 As Integer
Dim intOP4 As Integer
Dim intOP5 As Integer

Private Sub butPlay_Click(sender As System.Object, e As System.EventArgs) Handles butPlay.Click
    intM1 = rand.Next(1, 11)
    intM2 = rand.Next(1, 11)
    intM3 = rand.Next(1, 11)
    intM4 = rand.Next(1, 11)
    intM5 = rand.Next(1, 11)

    intOP1 = rand.Next(1, 11)
    intOP2 = rand.Next(1, 11)
    intOP3 = rand.Next(1, 11)


    lblPlayV.Text = 1
    lblCounter.Text = 1
    butPlay.Text = "Replay"

    lblM1.Text = intM1
    lblM2.Text = intM2
    lblM3.Text = intM3


    lblOP1.Text = intOP1
    lblOP2.Text = intOP2
    lblOP3.Text = intOP3


    lblM1.Visible = True
    lblM2.Visible = True
    lblM3.Visible = False
    lblM4.Visible = False
    lblM5.Visible = False

End Sub

Private Sub butHit_Click(sender As System.Object, e As System.EventArgs) Handles butHit.Click
    If lblPlayV.Text = 1 Then

        If lblCounter.Text = 1 Then
            lblM3.Visible = True

            If intM1 + intM2 + intM3 = 21 Then
                If intOP1 + intOP2 + intOP3 = 21 Then
                    MsgBox("21 : Draw")
                Else
                    MsgBox("21 : You Win")
                End If

            ElseIf intM1 + intM2 + intM3 > 21 Then
                If intOP1 + intOP2 + intOP3 > 21 Then
                    MsgBox("Bust : Draw")
                Else
                    MsgBox("Bust : You Lose")
                End If

            Else

            End If

        End If

        '<<<<<<<<<<<<<<<<<<<<<<'
        If lblCounter.Text = 2 Then
            lblM4.Visible = True

            If intM1 + intM2 + intM3 + intM4 = 21 Then
                If intOP1 + intOP2 + intOP3 = 21 Then
                    MsgBox("21 : Draw")
                Else
                    MsgBox("21 : You Win")
                End If

            ElseIf intM1 + intM2 + intM3 + intM4 > 21 Then
                If intOP1 + intOP2 + intOP3 > 21 Then
                    MsgBox("Bust : Draw")
                Else
                    MsgBox("Bust : You Lose")
                End If

            Else

            End If

        End If

        '<<<<<<<<<<<<<<<<<<<<<<'
        If lblCounter.Text = 3 Then
            lblM5.Visible = True

            If intM1 + intM2 + intM3 + intM4 + intM5 = 21 Then
                If intOP1 + intOP2 + intOP3 = 21 Then
                    MsgBox("21 : Draw")
                Else
                    MsgBox("21 : You Win")
                End If

            ElseIf intM1 + intM2 + intM3 + intM4 + intM5 > 21 Then
                If intOP1 + intOP2 + intOP3 > 21 Then
                    MsgBox("Bust : Draw")
                Else
                    MsgBox("Bust : You Lose")
                End If

            Else
                If intM1 + intM2 + intM3 + intM4 + intM5 = intOP1 + intOP2 + intOP3 Then
                    MsgBox("Draw")

                ElseIf intM1 + intM2 + intM3 + intM4 + intM5 > intOP1 + intOP2 + intOP3 Then
                    MsgBox("You Win")

                Else
                    MsgBox("You Lose")
                End If
            End If
            lblCounter.Text = 0
            lblPlayV.Text = 0
        End If

        '<<<<<<<<<<<<<<<<<<<<<<'
    End If

    lblCounter.Text = lblCounter.Text + 1
End Sub

Private Sub ButStand_Click(sender As System.Object, e As System.EventArgs) Handles ButStand.Click
    If lblPlayV.Text = 1 Then
        lblPlayV.Text = 0

        If lblM3.Visible = False Then
            If intM1 + intM2 = intOP1 + intOP2 + intOP3 Then
                MsgBox("Tie")

            ElseIf intM1 + intM2 > intOP1 + intOP2 + intOP3 Then
                MsgBox("You Win")
            Else
                MsgBox("You Lose")
            End If
        End If

        If lblCounter.Text = 1 Then
            If intM1 + intM2 + intM3 = intOP1 + intOP2 + intOP3 Then
                MsgBox("Tie")

            ElseIf intM1 + intM2 + intM3 > intOP1 + intOP2 + intOP3 Then
                MsgBox("You Win")
            Else
                MsgBox("You Lose")
            End If
        End If

        If lblCounter.Text = 2 Then
            If intM1 + intM2 + intM3 + intM4 = intOP1 + intOP2 + intOP3 Then
                MsgBox("Tie")

            ElseIf intM1 + intM2 + intM3 + intM4 > intOP1 + intOP2 + intOP3 Then
                MsgBox("You Win")
            Else
                MsgBox("You Lose")
            End If
        End If

        If lblCounter.Text = 3 Then
            If intM1 + intM2 + intM3 + intM4 + intM5 = intOP1 + intOP2 + intOP3 Then
                MsgBox("Tie")

            ElseIf intM1 + intM2 + intM3 + intM4 + intM5 > intOP1 + intOP2 + intOP3 Then
                MsgBox("You Win")
            Else
                MsgBox("You Lose")
            End If
        End If

    End If
End Sub