2015-10-06 38 views
-1

好吧,那麼當我選中或取消選中時,如何顯示摘要部分? 礦井因某種原因不能正常工作。我錯過了什麼嗎?我必須讓學生有能力控制信息的顯示。VB.NET如何不使用隨機生成的數字從textBoxResult的較小數中減去較大數字

公共類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 

末級

+1

您將獲得更好的反應,如果你能展示一些代碼,你到目前爲止 –

回答

0

確定做計算。這應該這樣做..壽當然如果LBL2數量是最大可能的隨機數(10或99)LBL1永遠不能做大的,所以編寫的代碼可能會導致lbl1和lbl2都是10或99. Anyho繼承代碼

ok ive在我的電腦上重新創建應用程序 - 原始代碼中有一些錯誤,雖然它看起來不錯一眼就知道了,例如在開始的時候,當你給lbl指定一個「+」時,你在它的任一側都有一個空格,但在檢查答案邏輯中沒有空格。當一個名爲「option explicit」的視覺工作室opton被打開時,對字符串的cint也不起作用 - 讓它鼓勵更好的編程:)也因爲這個「顯式選項,你會看到ive在一些地方添加了」.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 
+0

行。對於檢查回答按鈕,我認爲一旦隨機數字生成正確 - 用戶必須制定加法或減法並在文本框中輸入答案。基於此,在幾分鐘內在上面的代碼的末尾添加一些代碼 –

+0

非常感謝!我正在嘗試與你的現在比較。 :D –

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

實際上並不是問題的答案是問寫的。而且,它的功能相當低效。 – jmcilhinney

+0

謝謝,我會嘗試這 –

0

可以提示用戶來計算兩個數的差(無符號(+/-))。那麼你可以使用Math.Abs()

intResult = Math.Abs(Val(lbl1.Text)-Val(lbl2.Text)) 
+0

我仍然不能使用Mat.Abs,因爲我還沒有學習它。但感謝您幫助 –

相關問題