vb如何设计一个加减运算测试器 - 爱问答

(爱问答)

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

 

相关标签:运算

下一篇:如何在窗体中设计一个Shape1(形状)控件

上一篇:visualstudio2010简体中文编译出来的程序怎么在繁体环境运行

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?