在窗体form1上画一个label1显示算式、一个text1输入答案,一个command1(更换命题)和一个command2(提交答案).然后将以下代码黏贴到form1代码窗运行.你自己的代码做了部分调整放在command1里了:
Dim shu1 As Long, shu2 As Long, fuhao As String
Private Sub Command1_Click()
Randomize
shu1 = Int((999 - 100 + 1) * Rnd + 100)
shu2 = Int((999 - 100 + 1) * Rnd + 100)
fuhao = Mid("+-×÷", Fix(Rnd() * 4 + 1), 1)
Label1.Caption = CStr(shu1) fuhao CStr(shu2) " ="
Text1.Text = ""
Command2.Enabled = False
If Text1.Visible = True Then Text1.SetFocus
End Sub
Private Sub Command2_Click()
Select Case fuhao
Case "+"
If Val(Text1.Text) = shu1 + shu2 Then
MsgBox "答案正确": Command1_Click
Else
MsgBox "答案错误"
End If
Case "-"
If Val(Text1.Text) = shu1 - shu2 Then
MsgBox "答案正确": Command1_Click
Else
MsgBox "答案错误"
End If
Case "×"
If Val(Text1.Text) = shu1 * shu2 Then
MsgBox "答案正确": Command1_Click
Else
MsgBox "答案错误"
End If
Case "÷"
If Val(Text1.Text) = shu1 / shu2 Then
MsgBox "答案正确": Command1_Click
Else
MsgBox "答案错误"
End If
End Select
End Sub
Private Sub Form_Load()
Text1.Text = ""
Command1_Click
End Sub
Private Sub Text1_Change()
If Text1.Text <> "" Then Command2.Enabled = True Else Command2.Enabled = False
End Sub