乘法游戏的计数器

时间:2013-06-11 14:24:49

标签: vb.net random

Public Class Form1
' num1 and num2 now generate a number between 1 and 10
Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))
Dim counter As Integer
Dim num2 As Integer = CInt(Int((10 * Rnd()) + 1))

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



End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ' Generates a new question everytime the button is clicked
    num1 = CInt(Int((10 * Rnd()) + 1))
    num2 = CInt(Int((10 * Rnd()) + 1))
    'Displays question in textbox2
    TextBox2.Text = num1 & "*" & num2
End Sub

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    If TextBox1.Text = num1 * num2 Then
        ' If they get the answer right, they recieve postive feedback
        Label2.Text = "Correct!!11"
    Else
        ' Any other answer results in negative feedback
        Label2.Text = "Incorrect, sorry about

这个乘法游戏正在发挥作用。我只需要一种让游戏用户能够看到他们错误和正确的问题的方法。我需要实现一个计数器,但大多数在线指南都证明是无用的。我决定发布代码,看看是否有人可以帮助我。

2 个答案:

答案 0 :(得分:0)

您可以声明2个新变量来存储错误和正确的问题值。

Dim wrongQuestions as Integer = 0
Dim correctQuestions as Integer = 0

  If TextBox1.Text = num1 * num2 Then
        ' If they get the answer right, they recieve postive feedback
        Label2.Text = "Correct!!11"
        correctQuestions += 1
    Else
        ' Any other answer results in negative feedback
        Label2.Text = "Incorrect, sorry about"
        wrongQuestions += 1

然后使用

 msgbox("Correct Questions: " &  correctQuestions & " Incorrect Questions: " & wrongQuestions )

答案 1 :(得分:0)

使用随机和用户输入错误检查

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Me.AcceptButton = Button2
    Button1.PerformClick()
End Sub

Dim prng As New Random
Dim answer As Integer
Dim incorrect As Integer = 0
Dim correct As Integer = 0

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Generates a new question everytime the button is clicked
    Dim num1 As Integer = prng.Next(1, 11)
    Dim num2 As Integer = prng.Next(1, 11)
    TextBox2.Text = String.Format("{0} * {1}", num1, num2)
    answer = num1 * num2
    TextBox1.Text = ""
    TextBox1.Select()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'check answer
    Dim useranswer As Integer
    If Integer.TryParse(TextBox1.Text, useranswer) Then
        If useranswer = answer Then
            correct += 1
            Label2.Text = "Correct  "
        Else
            incorrect += 1
            Label2.Text = "Incorrect"
        End If
        Label2.Text &= String.Format("     Totals: correct = {0}, incorrect = {1}", _
                                     correct, incorrect)
        Button1.PerformClick()
    Else
        Label2.Text = "answer should be numeric"
    End If
End Sub