我是VB的新手,需要我目前的学校作业帮助。作业的要点是使用程序和选择案例来制作一个程序,该程序将提示用户回答7个短语的True或False并显示其结果。我有一个正确显示数字 numRight 的问题。当我运行它时,numRight最多只能达到2。
e.g。用户点击true,true,false,true,false,true
numRight = 2,实际上这些都是正确答案。
有人能解释下面代码中的错误吗?谢谢!
Public Class CIS14Lab4
'Publicly shared variables
Dim counter As Integer = 0
Dim numRight As Integer = 0
Private Sub btnMain_Click(sender As Object, e As EventArgs) Handles btnMain.Click
counter += 1
DisplayPhrase(counter) 'Used for displaying the phrase and adding to the numRight variable
DisplayResult(numRight) 'Used for displaying the result to the user
grpBoxTorF.Enabled = True
radTrue.Checked = False
radFalse.Checked = False
lblDebugRight.Text = numRight 'Debugging purposes; displays numRight
End Sub
Sub DisplayPhrase(c As Integer)
Select Case c 'Passed from counter
Case 1 'True
txtResult.Text = "1. The squeaky wheel gets the grease."
TrueRight()
Case 2 'True
txtResult.Text = "2. Cry and you cry alone."
TrueRight()
Case 3 'False
txtResult.Text = "3. Opposites attract."
FalseRight()
Case 4 'False
txtResult.Text = "4. Spare the rod and spoil the child."
FalseRight()
Case 5 'True
txtResult.Text = "5. Actions speak louder than words."
TrueRight()
Case 6 'False
txtResult.Text = "6. Familiarity breeds contempt."
FalseRight()
Case 7 'True
btnMain.Text = "Results:"
txtResult.Text = "7. Marry in haste, repent at leisure."
TrueRight()
End Select
End Sub
Sub DisplayResult(r As Integer)
If counter = 8 Then
Select Case r 'Passed from numRight
Case 7
txtResult.Text = "Perfect 7/7!"
Case 5 To 6
txtResult.Text = "Excellent!"
Case Is < 5
txtResult.Text = "You might want to consider taking " &
"Psychology 101."
End Select
End If
End Sub
Sub TrueRight() 'Sub proc for "True" answers that are correct.
If radTrue.Checked Then
numRight += 1
End If
End Sub
Sub FalseRight() 'SUb proc for "False" answers that are correct
If radFalse.Checked Then
numRight += 1
End If
End Sub
End Class
答案 0 :(得分:0)
解决! @LarsTech想出了我的问题。这对我来说是一个简单的疏忽。只需移动&#34;计数器+ = 1&#34; 在之后,程序允许它正确计数+正确添加数字。再次感谢!