2013-06-11 10 views
-2
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 

Эта игра умножения функционирует должным образом. Мне просто нужен способ, чтобы пользователь игры мог видеть, сколько вопросов они получили неправильно и правильно. Мне нужно создать счетчик, но большинство онлайн-гидов оказались бесполезными. Я решил просто опубликовать код и посмотреть, сможет ли кто-нибудь помочь мне.Счетчик для умножения игры

ответ

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) 
0

Использование Random и с ошибкой ввода пользователем проверки

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 
Смежные вопросы