帮我看看教科书上的一个例子
来源:百度知道 编辑:UC知道 时间:2024/08/20 20:26:06
Private Sub Command1_Click()
c = Val(Text1.Text)
If c = a + b Then
MsgBox "正确!", vbOKOnly, "提示"
a = Int(10 * Rnd): b = Int(10 * Rnd)
Label1.Caption = Str(a) & "+" & Str(b) & "="
Else
MsgBox "错误!", vbOKOnly + vbInformation, "提示"
End If
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Form_Load()
Form1.Show
Dim a As Integer
Dim b As Integer
Dim c As Integer
Randomize
a = Int(10 * Rnd): b = Int(10 * Rnd)
Label1.Caption = Str(a) & "+" & Str(b) & "="
Text1.Text = ""
Text1.SetFocus
Command1.Caption = "确认"
End Sub
是加法运算,我是按照书上打的 ,但怎么不行呢 ?
哪里错了阿
c = Val(Text1.Text)
If c = a + b Then
MsgBox "正确!", vbOKOnly, "提示"
a = Int(10 * Rnd): b = Int(10 * Rnd)
Label1.Caption = Str(a) & "+" & Str(b) & "="
Else
MsgBox "错误!", vbOKOnly + vbInformation, "提示"
End If
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Form_Load()
Form1.Show
Dim a As Integer
Dim b As Integer
Dim c As Integer
Randomize
a = Int(10 * Rnd): b = Int(10 * Rnd)
Label1.Caption = Str(a) & "+" & Str(b) & "="
Text1.Text = ""
Text1.SetFocus
Command1.Caption = "确认"
End Sub
是加法运算,我是按照书上打的 ,但怎么不行呢 ?
哪里错了阿
书上不可能是这样的,是你写错了,应该是这样的: (你看看有什么区别)
Dim a As Integer
Dim b As Integer
Dim c As Integer
Private Sub Command1_Click()
c = Val(Text1.Text)
If c = a + b Then
MsgBox "正确!", vbOKOnly, "提示"
a = Int(10 * Rnd): b = Int(10 * Rnd)
Label1.Caption = Str(a) & "+" & Str(b) & "="
Else
MsgBox "错误!", vbOKOnly + vbInformation, "提示"
End If
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Form_Load()
Form1.Show
Randomize
a = Int(10 * Rnd): b = Int(10 * Rnd)
Label1.Caption = Str(a) & "+" & Str(b) & "="
Text1.Text = ""
Text1.SetFocus
Command1.Caption = "确认"
End Sub
a,b的定义在form_load中,所以a,b的赋值只有在这里有效,到了command1_click中a,b 的值就又成了空值。所以有误。将a,b定义为全局变量就可以了。改正后的程式如下:
Dim a As Integer
Dim b As Integer
Dim c As Inte