VB.NET如何使用随机生成的数字从textBoxResult中减去较小数字中的较大数字

时间:2015-10-06 05:55:23

标签: vb.net

好的,那么当我选中或取消选中时,如何为显示摘要执行此部分? 我的某些原因并不起作用。我错过了什么吗?我必须让学生能够控制信息的显示。

Public Class MathPractice

Private Sub rbAddition_CheckedChanged(sender As Object, e As EventArgs) Handles rbAddition.CheckedChanged
    'Change the label to plus
    lblPlus.Text = " + "
End Sub

Private Sub rbSubtraction_CheckedChanged(sender As Object, e As EventArgs) Handles rbSubtraction.CheckedChanged
    'Change the label to minus        
    lblPlus.Text = "-"
    Dim intlbl1 As Integer = Val(lbl1.Text)
    Dim intlbl2 As Integer = Val(lbl2.Text)
    If rbSubtraction.Checked Then
        Do While intlbl1 < intlbl2
            If rbGrade1.Checked Then
                intlbl1 = Random.Next(1, 10)
            ElseIf rbGrade2.Checked Then
                intlbl1 = Random.Next(10, 99)
            End If
        Loop
        lbl1.Text = intlbl1.ToString
    End If
End Sub

Private Sub rbGrade1_CheckedChanged(sender As Object, e As EventArgs) Handles rbGrade1.CheckedChanged
    Dim random As New Random()
    'Get random numbers between 1 and 10.
    If rbGrade1.Checked Then
        lbl1.Text = random.Next(1, 10).ToString
        lbl2.Text = random.Next(1, 10).ToString
    End If
End Sub

Private Sub rbGrade2_CheckedChanged(sender As Object, e As EventArgs) Handles rbGrade2.CheckedChanged
    Dim random As New Random()
    random.Next(10, 99)
    'Get random numbers between 10 and 99.
    If rbGrade2.Checked Then
        lbl1.Text = random.Next(10, 99).ToString
        lbl2.Text = random.Next(10, 99).ToString
    End If
End Sub

Private Sub btnCheckAnswer_Click(sender As Object, e As EventArgs) Handles btnCheckAnswer.Click
    'Check the answer whether it is right or wrong
    'Display a message box showing right or wrong
    Dim intlbl1 As Integer = Val(lbl1.Text)
    Dim intlbl2 As Integer = Val(lbl2.Text)
    Dim intResult As Integer = Val(txtBoxResult.Text)
    Dim btnCheckAnswer As String
    Select Case lblPlus.Text
        Case "+"
            If intResult = intlbl1 + intlbl2 Then
                btnCheckAnswer = "Correct."
            Else
                btnCheckAnswer = "Sorry. Try Again."
            End If
        Case "-"
            If intResult = intlbl1 - intlbl2 Then
                btnCheckAnswer = "Correct."
            Else
                btnCheckAnswer = "Sorry. Try Again."
            End If
    End Select
End Sub

Private Sub chkBoxDisplaySummary_CheckedChanged(sender As Object, e As EventArgs) Handles chkBoxDisplaySummary.CheckedChanged
    'Display or hide information      
    If chkBoxDisplaySummary.Checked Then
        GroupBox2.Visible = True
    Else
        GroupBox2.Visible = False
    End If
End Sub

Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

结束班

3 个答案:

答案 0 :(得分:0)

if cint(lbl1.Text)>=cint(lbl2.Text) then
 intResult = cint(lbl1.Text)-cint(lbl2.Text) 
else
intResult = cint(lbl2.Text)-cint(lbl1.Text)
end if

答案 1 :(得分:0)

您可以提示用户计算两个数字之间的差异(无符号(+/-))。然后你可以使用Math.Abs()

进行计算
intResult = Math.Abs(Val(lbl1.Text)-Val(lbl2.Text))

答案 2 :(得分:0)

好吧..这应该这样做..当然,如果lbl2中的数字是可能的随机数(10或99)中最大的,lbl1永远不会更大,所以编写的代码可能会导致lbl1和lbl2都是10或99.Anyhoe是代码

好吧我已经在我的电脑上重新创建了应用程序 - 原始代码中有一些错误,虽然它看起来一目了然,却把事情丢掉了 - 例如在开始时分配了一个&#34; +&# 34;对于lbl,你在它的两侧有一个空格,但在检查答案逻辑中没有空格。当视觉工作室opton调用&#34;选项显式&#34;时,字符串上的cint也不起作用。打开 - 让它鼓励更好的编程:)同样因为这个&#34;选项显式,你也会看到我在几个地方添加&#34; .ToString

好的,这就是我所拥有的 - 我认为你的两组单选按钮位于两个独立的组框中,或者代码永远不会有效:)

查看下面的代码,并将其与原始代码并排比较。希望这可以帮助。如果确实如此,请不要忘记加号: - ))

Private Sub rbAddition_CheckedChanged(sender As Object, e As EventArgs) Handles rbAddition.CheckedChanged
    lblPlus.Text = "+"
End Sub

Private Sub rbSubtraction_CheckedChanged(sender As Object, e As EventArgs) Handles rbSubtraction.CheckedChanged
    lblPlus.Text = "-"
    Dim intlbl1 As Integer = Val(lbl1.Text)
    Dim intlbl2 As Integer = Val(lbl2.Text)
    If rbSubtraction.Checked Then
        Do While intlbl1 < intlbl2
            If rbGrade1.Checked Then
                intlbl1 = random.Next(1, 10)
            ElseIf rbGrade2.Checked Then
                intlbl1 = random.Next(10, 99)
            End If
        Loop
        lbl1.Text = intlbl1.ToString
    End If
End Sub

Private Sub rbGrade1_CheckedChanged(sender As Object, e As EventArgs) Handles rbGrade1.CheckedChanged
    Dim random As New Random()
    'Get random numbers between 1 and 10.
    If rbGrade1.Checked Then
        lbl1.Text = random.Next(1, 10).ToString
        lbl2.Text = random.Next(1, 10).ToString
    End If
End Sub

Private Sub rbGrade2_CheckedChanged(sender As Object, e As EventArgs) Handles rbGrade2.CheckedChanged
    Dim random As New Random()
    random.Next(10, 99)
    'Get random numbers between 10 and 99.
    If rbGrade2.Checked Then
        lbl1.Text = random.Next(10, 99).ToString
        lbl2.Text = random.Next(10, 99).ToString
    End If
End Sub

Private Sub btnCheckAnswer_Click(sender As Object, e As EventArgs) Handles btnCheckAnswer.Click
    Dim intlbl1 As Integer = Val(lbl1.Text)
    Dim intlbl2 As Integer = Val(lbl2.Text)
    Dim intResult As Integer = Val(txtboxResult.Text)
    Dim isTheAnswerCorrect As String
    Select Case lblPlus.Text
        Case "+"
            If intResult = intlbl1 + intlbl2 Then
                isTheAnswerCorrect = "Correct!"
            Else
                isTheAnswerCorrect = "Sorry. Try Again"
            End If
        Case "-"
            If intResult = intlbl1 - intlbl2 Then
                isTheAnswerCorrect = "Correct!"
            Else
                isTheAnswerCorrect = "Sorry. Try Again"
            End If
    End Select
    MsgBox(isTheAnswerCorrect)
End Sub

Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub