如何根据“if"语句增加标签内的数字 [英] How to increase a number inside a label according to a 'if' statement

查看:23
本文介绍了如何根据“if"语句增加标签内的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据if"语句增加标签内的数字?这是我的代码:

How do I increase a number inside a label according to a 'if' statement? Here is my code:

Dim uservalue As Integer
uservalue = TextBox1.Text

If tbxOperator.Text = "+" Then
    If uservalue = number + number1 Then
        MessageBox.Show("Correct")
    Else
        MessageBox.Show("Incorrect")
    End If
End If

If tbxOperator.Text = "-" Then
    If uservalue = number - number1 Then
        MessageBox.Show("Correct")
    Else
        MessageBox.Show("Incorrect")
    End If
End If

Dim rn As Random
rn = New Random
number = rn.Next(1, 9)
number1 = rn.Next(1, 9)
tbxOne.Text = number.ToString
tbxTwo.Text = number1.ToString

number2 = rn.Next(1, 3)
If number2 = 1 Then
    tbxOperator.Text = "+"
Else
    tbxOperator.Text = "-"
End If

我本质上有一些功能代码,可以使用+"或-"运算符生成一个简单的总和,如果用户键入正确答案,则会出现一个框,提示正确".我想做的是当用户得到正确答案时,他们会在名为Points"的标签中获得一分.有人可以帮我做这件事吗?

I essentially have some functioning code that will generate a simple sum with a '+' or '-' operator and if the user types the correct answer a box appears saying 'correct'. What I would like to do is make it so when the user gets a correct answer, they are awarded a point in a label called 'Points'. Could somebody assist me in doing this?

推荐答案

您可以调整您的代码(这称为重构") 这样您就不必多次编写相同的内容,例如 MessageBox.Show("Correct") 行.

You can adjust your code (this is called "refactoring") so that you don't have to write the same thing more than once, for example the MessageBox.Show("Correct") lines.

此外,您可以使用选择案例 替换一些 If...Then 语句 - 目前这只是一个小小的改进,但是如果你想添加,比如乘法和除法,那么它会让你变得更简单这样做.

Also, you can use Select Case to replace some of the If...Then statements - it is only a slight improvement at the moment, but if you wanted to add, say multiplication and division then it makes it much simpler to do so.

当用户得到正确答案时,您需要为他们的分数加一个(即增加)并更新标签.因为我们重构了代码,这可以在一个地方完成而不是你必须用代码做的两个地方:

When the user gets an answer correct, you need to add one to (i.e. increment) their score and update the label. Because we have refactored the code, this can be done in just one place instead of the two places which you would have had to do with the code as it was:

Option Infer On ' allow the compiler to figure out data types in Dim statements.
Option Strict On ' make sure all variable types match up.

Public Class Form1

    Dim rn As New Random
    Dim userPoints As Integer
    Dim number1 As Integer
    Dim number2 As Integer
    Dim testOperator As String

    Private Sub bnTestMe_Click(sender As Object, e As EventArgs) Handles bnTestMe.Click
        ' select two random numbers for the test
        number1 = rn.Next(1, 9)
        number2 = rn.Next(1, 9)

        tbxOne.Text = number1.ToString()
        tbxTwo.Text = number2.ToString()

        ' select a random operator for the test
        Dim operatorChoice = rn.Next(1, 3)

        Select Case operatorChoice
            Case 1
                testOperator = "+"
            Case 2
                testOperator = "-"
        End Select

        tbxOperator.Text = testOperator

    End Sub

    Private Sub bnCheckIt_Click(sender As Object, e As EventArgs) Handles bnCheckIt.Click
        Dim uservalue As Integer = CInt(TextBox1.Text)
        Dim isCorrect As Boolean

        ' check if the user got it correct
        Select Case testOperator
            Case "+"
                isCorrect = (uservalue = number1 + number2)
            Case "-"
                isCorrect = (uservalue = number1 - number2)
        End Select

        ' perform appropriate actions depending on if the user was correct.
        If isCorrect Then
            userPoints += 1
            MessageBox.Show("Correct")
            lblPoints.Text = userPoints.ToString()
        Else
            MessageBox.Show("Incorrect")
        End If

    End Sub

End Class

我使用了与您展示的略有不同的变量名称,以便它们更加一致.我使用名为bnTestMe"的按钮向用户展示测试,并使用名为bnCheckIt"的按钮检查答案.

I used slightly different variable names than you showed so that they are more consistent. I used a button named "bnTestMe" to present the test to the user, and a button named "bnCheckIt" to check the answer.

userPoints += 1userPoints = userPoints + 1 的缩写.(当 += 不用于简单的局部变量时,有一些技术要点:复合分配 - 你可能还不需要知道.)

userPoints += 1 is short for userPoints = userPoints + 1. (There are some technical points about += when it isn't used on simple local variables: Compound Assignment - which you probably don't need to know about yet.)

我将测试的数字和运算符存储在变量中,这样用户就不会通过输入其他值来作弊.

I stored the numbers and the operator for the test in variables so that there is no chance of the user cheating by typing in other values.

isCorrect = (uservalue = number1 + number2) 是这样工作的:(uservalue = number1 + number2) 给出了一个 Boolean(即真或假)结果,就像您使用 If (uservalue = number1 + number2) 那么....该结果被分配给 isCorrect 变量.然后我们可以稍后使用 isCorrect 来查看用户是否正确.

The line isCorrect = (uservalue = number1 + number2) works like this: (uservalue = number1 + number2) gives a Boolean (i.e. True or False) result, like when you use If (uservalue = number1 + number2) Then.... That result is assigned to the isCorrect variable. Then we can use isCorrect later to see if the user got it right.

这篇关于如何根据“if"语句增加标签内的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆