vb如何设计一个加减运算测试器
单击“出题”按钮,随机生成测试题,在文本框输入题目答案,单击“统计”按钮时,显示答题对错结果
在VB6中新建一个窗体,并用如下代码替换窗体中原有的所有代码,即可达到你想要的结果:
Option Explicit
Private WithEvents MakeQuestion As VB.CommandButton
Private WithEvents Statistics As VB.CommandButton
Private WithEvents Result As VB.TextBox
Private QuestionBottom As Long, qRes As Long
Private Total As Long, OK As Long
Private Sub Form_Load()
Dim W As Long, H As Long, WD As Long, HD As Long, T As String, TW As Long, TH As Long, TL As Long, TT As Long
Me.ScaleMode = 1
WD = Me.Width - Me.ScaleWidth: HD = Me.Height - Me.ScaleHeight
W = Me.ScaleX(640, 3, 1) + WD: H = Me.ScaleY(480, 3, 1) + HD
Me.Move (Screen.Width - W) * 0.5, (Screen.Height - H) * 0.5, W, H
Me.Caption = "加减运算测试器"
Me.ScaleMode = 3
Set Me.Icon = Nothing
With Me.Font
.Name = "微软雅黑"
.Size = 24
End With
Set MakeQuestion = Me.Controls.Add("VB.CommandButton", "MakeQuestion")
Set Result = Me.Controls.Add("VB.TextBox", "Result")
Set Statistics = Me.Controls.Add("VB.CommandButton", "Statistics")
T = "请输入结果:": TW = Me.TextWidth(T): TH = Me.TextHeight(T)
Me.AutoRedraw = True
With MakeQuestion
.Move (Me.ScaleWidth - 480) * 0.5, Me.ScaleHeight - 72, 240, 64
Me.CurrentX = .Left: Me.CurrentY = .Top - TH * 1.5: Me.Print T;: TL = Me.CurrentX: TT = Me.CurrentY
.Caption = "出题(&Q)"
Set .Font = Me.Font
.Visible = True
End With
With Statistics
.Move MakeQuestion.Left + 240, MakeQuestion.Top, 240, 64
.Caption = "统计(&S)"
Set .Font = Me.Font
.Visible = True
End With
QuestionBottom = TT - 2
With Result
.Move TL, QuestionBottom, 480 - TW, Me.TextHeight(T)
.Text = ""
.Visible = True
End With
Me.Show
DoEvents
MakeQuestion.SetFocus
End Sub
Private Sub MakeQuestion_Click()
Dim A As Long, B As Long, T As String
A = Rnd * 10001
B = Rnd * 20001 - 10000
T = Trim$(A) & IIf(Abs(B) = B, "+", "") & Trim$(B) & "=?"
qRes = A + B
Me.Line (0, 0)-(Me.ScaleWidth, QuestionBottom), Me.BackColor, BF
Me.CurrentX = (Me.ScaleWidth - Me.TextWidth(T)) * 0.5
Me.CurrentY = (QuestionBottom - Me.TextHeight(T)) * 0.5
Me.Print T
Total = Total + 1
Result.SetFocus
End Sub
Private Sub Result_GotFocus()
With Result
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
Private Sub Result_KeyPress(KeyAscii As Integer)
Dim T As String
T = Trim$(Result.Text)
Select Case KeyAscii
Case vbKeyBack
Case vbKey0 To vbKey9
If Len(T) > 5 And Result.SelLength = 0 Then KeyAscii = 0
Case vbKeyReturn
If Total > 0 Then If Len(T) > 0 Then Statistics.SetFocus Else MsgBox "请先输入结果", vbCritical, "错误" Else MsgBox "请先单击“出题”按钮", vbInformation, "错误": MakeQuestion.SetFocus
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Statistics_Click()
Dim R As Long, T(1 To 4) As String, L(1 To 4) As Long, I As Long, TH As Long, TWC As Long, TT As Long, TL As Long
If Total > 0 Then
If Len(Trim$(Result.Text)) > 0 Then
R = Val(Result.Text)
If R = qRes Then OK = OK + 1
T(1) = "题目总数:" & Trim$(Total): TH = Me.TextHeight(T(1))
T(2) = "答对次数:" & Trim$(OK)
T(3) = "答错次数:" & Trim$(Total - OK)
T(4) = "正确比率:" & Format$(OK / Total, "0%")
For I = 1 To 4: TWC = TWC + Me.TextWidth(T(I)): Next: TWC = TWC * 0.25
Me.Line (0, 0)-(Me.ScaleWidth, QuestionBottom), Me.BackColor, BF
TL = (Me.ScaleWidth - TWC) * 0.5: TT = (QuestionBottom - TH * 4) * 0.5
For I = 1 To 4
Me.CurrentX = TL: Me.CurrentY = TT: Me.Print T(I)
TT = TT + TH
Next
MakeQuestion.SetFocus
Else
MsgBox "请先输入结果", vbCritical, "错误"
End If
Else
MsgBox "请单击“出题”按钮", vbInformation, "错误"
MakeQuestion.SetFocus
End If
End Sub