VB 设计一个自动出题并判断正误的程序

VB 设计一个自动出题并判断正误的程序,第1张

Option Base 1

Dim a As Integer, b As Integer, c As Integer, n As Integer

Private Sub Command1_Click()

s = "+-×÷"

Text1.Text = ""绝侍

n = Int(Rnd(1) * 4) + 1

a = Int(9 * Rnd(1)) + 1

b = Int(9 * Rnd(1)) + 1

If n = 2 And a <b Then t = a: a = b: b = a

If n = 4 Then c = b: b = a: a = a * c

Label1.Caption = a &Mid(s, n, 1) &b &"="

End Sub

Private Sub Command2_Click()

Select Case n

Case 1

c = a + b

Case 2

c = a - b

Case 3

c = a * b

Case 4

c = a / b

End Select

If c = CInt(Text1.Text) Then

MsgBox ("对了,您耐虚真棒!")

Else

MsgBox ("错了,再努力!")

Text1.SetFocus

Text1.SelStart = 0

Text1.SelLength = Len(Text1.Text)

End If

End Sub

Private Sub Form_Load()

Label1.Caption = ""

Text1.Text = "昌宏燃"

Label1.FontSize = 20

Text1.FontSize = 20

End Sub

既然是选择题,那肯定有很多题,你可以在页面当中添加一个“下一题”的按钮,再把这个按钮的TabIndex属性搏卜设置为0,这样在显示页面时,基贺穗默认选中的是“下一题”的这拍轮个按钮,这样的页面才比较最合理。

PS:在做选择题这样的程序时,最好采用数据库编程,就是将题目答案都放在数据库中,每次执行时,题目都是从数据库中提取,这样即使题目有上百题,VB中只要有一个界面就行了,这样更加方便。

程序 *** 作方法:点击“出题”,将会显示题目。

点击“结束”,会把当前题目族带以及答案保存下来。

点击左边的label控件,会显示所有做过的题目,并且右边显示相关数据。

每次结束后,显示题目的label控件会被清空显示。

再次做题,点击“出题”即可。

如果没有填写任何答案或者没有出题就点击“结束”,那么程序将不做任何动作,也不会给出提示信息。

左边的label控件可以重复多次点击。

Option Explicit '在使用变量前必须定义变量

Dim QuestionCount As Integer '已做题目个数

Dim Correct As Integer '答对题目数

Dim strQuestions(19) As String '保存20道题目

Dim strOpr As String '保存运算符

Dim X, Y As Integer '保存待计算的2个数

Dim bMade As Boolean '是否出题

Private Sub CmdEnd_Click()

  If QuestionCount < 20 Then '最多只能做20道题目

    If TxtAnswer.Text <> "" And bMade Then '填写答案了并且出了题目才提交答案

      Dim result As Integer

      Select Case strOpr

       Case "+"

        result = X + Y

       Case "-"

        result = X - Y

       Case ChrW(215)

        result = X * Y

       Case ChrW(247)

        result = X / Y

      End Select

      strQuestions(QuestionCount) = LabShowQuestion.Caption & " " & TxtAnswer.Text

      If Val(TxtAnswer.Text) = result Then

        strQuestions(QuestionCount) = strQuestions(QuestionCount) & " √"

        Correct = Correct + 1

        bAnswer(QuestionCount) = True

      Else

        strQuestions(QuestionCount) = strQuestions(QuestionCount) & " ×"

        bAnswer(QuestionCount) = False

      End If

      QuestionCount = QuestionCount + 1

      LabShowQuestion.Caption = ""

      TxtAnswer.Text = ""

      TxtAnswer.Left = LabShowQuestion.Left + LabShowQuestion.Width + 100

      bMade = False

    End If

  Else

    MsgBox "最多只能做20道题目。", vbOKOnly, "提示"

  End If

End Sub

Private Sub CmdShowQuestion_Click()

  LabShowQuestion.Caption = MakeQuestion

  TxtAnswer.Left = LabShowQuestion.Left + LabShowQuestion.Width + 100

  TxtAnswer.SetFocus

End Sub

Private Function MakeQuestion() As String

  Dim opr As Integer '值域从0 到 3,分别表示加减乘除。

  bMade = True

  X = CInt(Rnd() * 1000) Mod 100 'CInt将Double或Float转换为Integer类型

  Y = CInt(Rnd() * 1000) Mod 100 'Rnd() * 1000随机生成一个数

  opr = Rnd() * 1000 Mod 4 '随机生成一个运算符

  If opr = 3 Then

    Y = IIf(Y = 0, 1, Y) '防止除数为0

  End If

  Select Case opr

   Case 0

    strOpr = "+"

  薯孙 Case 1

    strOpr = "-"

   Case 2

    strOpr = ChrW(215)

   Case 3

    strOpr = ChrW(247)

  End Select

  MakeQuestion = X & " " & strOpr & " " & Y & " ="

End Function

Private Sub Form_Load()

  Dim i As Integer

  QuestionCount = 0

  Correct = 0

  bMade = False

End Sub

Private Sub LabShowResult_Click()

  If QuestionCount > 0 Then

   数穗链 Dim i As Integer

    LabShowResult.Caption = ""

    For i = 0 To QuestionCount - 1

      LabShowResult.Caption = LabShowResult.Caption & strQuestions(i) & Chr(13)

    Next i

    TxtScore(0).Text = QuestionCount

    TxtScore(1).Text = Correct

    TxtScore(2).Text = QuestionCount - Correct

    TxtScore(3).Text = CInt(Correct / QuestionCount * 100) & "%"

  End If

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12281809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存