数学游戏-如何提出10个问题

时间:2018-11-29 10:44:47

标签: vb.net math messagebox

我正在做一个数学测验游戏,我想知道为什么当我得到正确的答案并如何将其设为10并停止运行然后跳出一个消息框询问用户想要的问题时,问题却不会变成“ NEW ONE”还是不能再玩?

Public Class Multiplication
Dim TotalQuestion As Integer
Dim CorrectAnswer As Integer
Dim WrongAnswer As Integer
Dim R As New Random
Dim numOne As Integer = R.Next(0, 10)
Dim numTwo As Integer = R.Next(1, 10)

Dim Ans As Integer = 0
Dim Tries As Integer = 0

Private Sub Multiplication_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Generate()
End Sub

Sub Generate()
    TotalQuestion = TotalQuestion + 1
    Dim Show As String
    Show = numOne & " x " & numTwo & " = "
    lblQuestionMUL.Text = Show
End Sub

Private Function Multiply(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
    Return num1 * num2
    Generate()
End Function

Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
    Integer.TryParse(lblQuestionMUL.Text, numOne & numTwo)
    Ans = Multiply(numOne, numTwo)
    If Val(txtAnswer.Text) = Ans Then
        CorrectAnswer = CorrectAnswer + 1
    Else
        WrongAnswer = WrongAnswer + 1
    End If
    lblCorrectAns.Text = CorrectAnswer
    lblWrongAns.Text = WrongAnswer
    txtAnswer.Clear()
    txtAnswer.Focus()
    Generate()
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

除了前面的注释,您还应该在Generate Sub(将声明保留为全局变量)中将numOne和numTwo变量设置为随机数。您的代码仅在开始时将它们设置为随机数一次。检查以下代码:

Public Class Multiplication

Dim TotalQuestion As Integer = 0
Dim CorrectAnswer As Integer
Dim WrongAnswer As Integer
Dim R As New Random
Dim NumOne As Integer
Dim NumTwo As Integer
Dim QuizCompleted As Boolean = False

Dim Ans As Integer = 0
Dim Tries As Integer = 0

Private Sub Multiplication_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Generate()

End Sub


Sub Generate()

    If TotalQuestion < 10 Then

        NumOne = R.Next(0, 10)
        NumTwo = R.Next(1, 10)

        TotalQuestion = TotalQuestion + 1
        Dim Show As String
        Show = NumOne & " x " & NumTwo & " = "
        lblQuestionMUL.Text = Show

    Else

        QuizCompleted = True

    End If

End Sub

Private Function Multiply(ByVal num1 As Integer, ByVal num2 As Integer) As Integer

    Generate()
    Return num1 * num2

End Function

Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click

    Integer.TryParse(lblQuestionMUL.Text, NumOne & NumTwo)
    Ans = Multiply(NumOne, NumTwo)
    If Val(txtAnswer.Text) = Ans Then
        CorrectAnswer = CorrectAnswer + 1
    Else
        WrongAnswer = WrongAnswer + 1
    End If
    lblCorrectAns.Text = CorrectAnswer
    lblWrongAns.Text = WrongAnswer
    txtAnswer.Clear()
    txtAnswer.Focus()

    If QuizCompleted Then

        MsgBox("Quiz Completed")

    End If

End Sub
End Class