感觉我写的代码比书讲的好多啦

分析:为了减少输入和增加试题内容的随机性,操作数和运算符通过随机函数Rnd产生,操作数的范围是1~10运算符1~4分别代表+-*/Rnd产生的数是在0~1之间的实数,为产生1~10的整数作为操作数,可通过Int(10*Rnd+1)表达式实现,运算符产生同理。

         本例共需要除窗体外的5个控件,其中标签控件1个,文本框1个,命令按钮2个,图形框1个,有关控件设置:

默认控件名

设置的控件名

标题

文本 

图形

 说明

Name

Caption

Text

Picture

Label1

lblExp

空白

无定义

无定义

显示产生的题目

Text1

txtInput

无定义

空白

无定义

Command1

cmdOk

确定

无定义

无定义

每题结束

Command2

cmdMark

计分

无定义

无定义

最后记分

Picture1

Picture1

无定义

无定义

空白

显示题目和结果

由于本例有三个事件过程,为了便于事件过程中的数据共享,在事件过程前定义了窗体级变量:

Dim Num1 As Integer, Num2 As Integer       ‘存放两个操作数

Dim Sexp As String                                                         

Dim Result As Single                                                       ‘计算结果

Dim NOk As Integer, NError As Integer                     ‘统计计算正确与错误

本例通过三个事件过程:

(1)       通过产生随机数生成题目

Private Sub Form_Load()

Dim NOp As Integer, Op As String * 1                        ‘存放运算符

Num1 = Int(10 * Rnd + 1)                                               ‘产生1~10之间的操作数

Num2 = Int(10 * Rnd + 1)                                               ‘产生1~10之间的操作数

NOp = Int(4 * Rnd + 1)                                                    ‘产生1~4之间的运算符代码

Select Case NOp                         ‘1~4之间的运算符代码转换成运算符,并进行四则运算

Case 1

Op = "+": Result = Num1 + Num2

Case 2

Op = "-": Result = Num1 - Num2

Case 3

Op = "*": Result = Num1 * Num2

Case 4

Op = "/": Result = Num1 / Num2

End Select

Sexp = Num1 & Op & Num2 & "="

lblExp = Sexp                                                            ‘显示生成题目

End Sub

(2)       txtInput文本框中输入计算结果,单击“确定”按钮,在图形框显示正确与否

Private Sub cmdOk_Click()                                                              

If Val(txtInput) = Result Then                                                ‘输入的结果与计算机计算的结果相同

Picture1.Print Sexp; txtInput; Tab(10); "Y"                        ‘显示正确符号Y

NOk = NOk + 1                                                                            ‘累计计算正确数

Else

Picture1.Print Sexp; txtInput; Tab(10); "X"                        ‘显示计算错误符号 X

NError = NError + 1                                                                  ‘累计计算错误数

End If

txtInput = ""

Form_Load                                                                                  ‘下一个题目生成

End Sub

(3)       计算所得的成绩

Private Sub CmdMark_Click()

Picture1.Print "____________"

Picture1.Print "一共计算" & Int(NOk + NError) & "道题"

Picture1.Print "得分" & Int(NOk / (NOk + NError) * 100)

End Sub

Dim x%, y%, p%

Dim a(0 To 10) As Integer

Dim b(0 To 10) As Integer

Dim s(1 To 10) As Integer

Private Sub Form_Load()

For i = 1 To 10

Label1.Item(i).Caption = ""

Label2.Item(i).Caption = ""

Label3.Item(i).Caption = ""

Label4.Item(i).Caption = ""

Text1.Item(i).Visible = False

Command1.Visible = False

Label5.Item(i).Visible = False

Next

End Sub

Private Sub Command2_Click()

Randomize

For i = 1 To 10

s(i) = Int(4 * Rnd) + 1

x = Int(9 * Rnd) + 1

y = Int(9 * Rnd) + 1

Select Case s(i)

Case 1

a(i) = x

b(i) = y

Label2.Item(i).Caption = "+"

Case 2

x = Int(9 * Rnd) + 1

y = Int(9 * Rnd) + 1

a(i) = x

b(i) = y

If a(i) < b(i) Then

p = a(i)

a(i) = b(i)

b(i) = p

End If

Label2.Item(i).Caption = "-"

Case 3

a(i) = x

b(i) = y

Label2.Item(i).Caption = "*"

Case 4

a(i) = x

b(i) = y

    If a(i) < b(i) Then

    p = a(i)

    a(i) = b(i)

    b(i) = p

    End If

    If a(i) Mod b(i) <> 0 Then

    i = i - 1

    Else

    Label2.Item(i).Caption = "/"

    End If

End Select

Label1.Item(i).Caption = a(i)

Label3.Item(i).Caption = b(i)

Label4.Item(i).Caption = "="

Text1.Item(i).Text = ""

Text1.Item(i).Visible = True

Next

Command2.Visible = False

Command1.Visible = True

End Sub

Private Sub Command1_Click()

Dim u%

Dim q(1 To 10) As Integer

Dim r(1 To 10) As Integer

For i = 1 To 10

If Text1.Item(i).Text <> "" Then

'If CInt(Text1.Item(i).Text) = ture Then

r(i) = CInt(Text1.Item(i).Text)

Else

r(i) = 26

End If

Select Case s(i)

Case 1

q(i) = a(i) + b(i)

Case 2

q(i) = a(i) - b(i)

Case 3

q(i) = a(i) * b(i)

Case 4

q(i) = a(i) / b(i)

End Select

Label5.Item(i).Visible = True

If r(i) = q(i) Then

u = u + 10

Label5.Item(i).Caption = "正确"

Else

Label5.Item(i).Caption = "错误"

End If

Next

Label6.Visible = True

Label6.Caption = "您的成绩" + CStr(u) + ""

End Sub

posted on 2007-12-01 05:31  超少  阅读(395)  评论(3编辑  收藏  举报