首页 > 技术文章 > 第二次作业: 四则运算的实现

yilinwei 原文

Visual Basic 实现随机四则运算

 

前提:

    老实的讲在学校里并没有教过Visual B,但是我用C-Free实在是没有办法独立写出来了,所以我把目标转向了Microsoft Visual Studio 2010,并且找到了《VB实例讲解》这本书里面找到了类似的例题就照着做了,到处修修改改之后还能用,感觉还不错^.^

 PS:

     VB用起来比C和C#都要简单,而且代码也要少很多!

实验要求:

使用随机函数与选择结构,实现动态的两位数的四则运算练习软件。

1、能够实现真正的动态出题(每次的出题内容不同)。

2、能够针对正确答案与错误答案提供多种评定语句。

3、能够统计每次练习的内容。

5、能够确定练习次数,并统计正确与错误的数量。

6、尽可能将基础控件的基本属性使用出来。

 

 

设计界面:

程序运行结果

Visual Basic实验四 随机四则运算软件 详解 - rsb_long - rsb_long的博客
启动界面

Visual Basic实验四 随机四则运算软件 详解 - rsb_long - rsb_long的博客

运行界面

1、关于时间控件的使用代码说明:

      此控件的内容属于后续章节的内容,但由于其功能简单,完全可以通过MSDN或基础控件的使用方式进行推理分析。该控件有两个重要属性需要设定:EnabledInterva

其中Enabled与基本控件的概念相同,都是用于对象是否可以执行(可以显示但不能使用),在程序启动时设置为True表示该控件可以使用。第二个属性用于表示该控件执行的时间间隔是多长时间。通常设置为100,它代表100毫秒。因此Interval设计的数字越大表示执行的间隔越长。它的事件只有一个,相关的MSDN解释如下图。

Visual Basic实验四 随机四则运算软件 详解 - rsb_long - rsb_long的博客

       在本程序的设计中,要求程序启动的时候,屏幕中出现一个由大变小,然后又由小变大的提示内容。相关的代码如下。此代码稍加修改可以变成让字幕左右飘动的样式。

Private Sub Timer1_Timer()

If fx = True Then    ‘fx为自定义的变量,用来控件是变大,还是变小,为逻辑变量

    If Label3.FontSize + 5 < 60 Then

        Label3.FontSize = Label3.FontSize + 5

    Else

        fx = False

    End If

Else

    If Label3.FontSize - 5 > 10 Then

        Label3.FontSize = Label3.FontSize - 5

    Else

        fx = True

    End If

End If

End Sub

2、系统初始化效果实现

       由设计要求的样式,可以发现大多数的控件在启动的时间并没有出现。因此显然当时的属性设置为VisabledFasle。在程序加载的过程中需要完成对基本控件的参数设定工作。

Private Sub Form_Load()

Randomize      ‘确认系统将真正随机组合数字

Form1.Caption = "Visual Basic实验4"

Label1.Caption = ""

Label2.Caption = "="

Label3.Caption = "随机四则运算练习"

Label4.Caption = "耿丹:韦艺林"

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Text4.Text = ""

Text5.Visible = False      ‘题目要求中没有出现,它的功能是用来显示源代码

Command1.Caption = "出题"

Command2.Caption = "检查"

Command3.Caption = "代码"

Command4.Caption = "退出"

Text1.Visible = False

Text2.Visible = False

Text3.Visible = False

Text4.Visible = False

Command1.Visible = False

Command2.Visible = False

Command3.Visible = False

Command4.Visible = False

Label1.Visible = False

Label2.Visible = False

End Sub

3、题目的随机出现方式

       题目设计的核心内容是出现题目后等待用户的操作。出题后,需要保存题目的运算方式。利用内存变量,在程序执行到End Sub后会自动放弃其中的内容。因此在通用程序行需要声明相关的变量。这样程序在其它过程中可以继续使用变量的内容。

(通用)部分的代码如下

Dim fx As Boolean, xz As Integer

出题部分的代码如下

Private Sub Command1_Click()

xz = Int(Rnd * 4 + 1)    xz表示产生的选择条件的类型

Text1.Text = Int(Rnd * 90 + 10)

Text2.Text = Int(Rnd * 90 + 10)

Text3.Text = ""

Select Case xz        ‘使用多分支结构,利用随机函数功能实现动态四则运算功能

Case 1

    Label1.Caption = ""

Case 2

    Label1.Caption = ""

Case 3

    Label1.Caption = "×"

Case Else               ‘注意case结构的最后一项应当是case else。避免出现考虑不完整

    Label1.Caption = "÷"

End Select

End Sub

4核心代码:

Private Sub Command2_Click()

If Text1.Text = "" Or Text2.Text = "" Then

    MsgBox "请先出题目然后再进行此动作", , "系统提示"

Else

    Select Case xz

    Case 1

        If Text3.Text = Val(Text1.Text) + Val(Text2.Text) Then

            jl = Int(Rnd * 5 + 1)   '选择5表示有五种可能的表扬方式

            Select Case jl

            Case 1

                MsgBox "今天发挥的不错"

            Case 2

                MsgBox "聪明的人,请继续努力"

            Case 3

                MsgBox "是你聪明还是运气不错"

            Case 4

                MsgBox "比较神奇的能力"

            Case Else

                MsgBox "判断相当的给力,继续努力"

            End Select

            Text4.Text = Text4.Text + Text1.Text + "+" + Text3.Text + "=" + Text3.Text + "正确" + vbCrLf

        Else

            jl = Int(Rnd * 5 + 1)

            Select Case jl

            Case 1

                MsgBox "没有看清题目的要求?"

            Case 2

                MsgBox "需要提高注意力了!!!!"

            Case Else

                MsgBox "你是不是需要休息一下了?成绩太差了"

            End Select

            Text4.Text = Text4.Text + Text1.Text + "+" + Text3.Text + "=" + Text3.Text + "错误" + vbCrLf

        End If

    Case 2

        If Text3.Text = Val(Text1.Text) - Val(Text2.Text) Then

            jl = Int(Rnd * 5 + 1)   '选择5表示有五种可能的表扬方式

            Select Case jl

            Case 1

                MsgBox "今天发挥的不错"

            Case 2

                MsgBox "聪明的人,请继续努力"

            Case Else

                MsgBox "判断相当的给力,继续努力"

            End Select

            Text4.Text = Text4.Text + Text1.Text + "+" + Text3.Text + "=" + Text3.Text + "正确" + vbCrLf

        Else

            jl = Int(Rnd * 5 + 1)

            Select Case jl

            Case 1

                MsgBox "没有看清题目的要求?"

            Case 2

                MsgBox "需要提高注意力了!!!!"

            Case 3

                MsgBox "如此简单还要出错????"

            Case 4

                MsgBox "能不能仔细一些!!!!!!!"

            Case Else

                MsgBox "你是不是需要休息一下了?成绩太差了"

            End Select

            Text4.Text = Text4.Text + Text1.Text + "+" + Text3.Text + "=" + Text3.Text + "错误" + vbCrLf

        End If

    Case 3

        If Text3.Text = Val(Text1.Text) * Val(Text2.Text) Then

            jl = Int(Rnd * 5 + 1)   '选择5表示有五种可能的表扬方式

            Select Case jl

            Case 1

                MsgBox "今天发挥的不错"

            Case 2

                MsgBox "聪明的人,请继续努力"

            Case 3

                MsgBox "是你聪明还是运气不错"

            Case 4

                MsgBox "比较神奇的能力"

            Case Else

                MsgBox "判断相当的给力,继续努力"

            End Select

            Text4.Text = Text4.Text + Text1.Text + "+" + Text3.Text + "=" + Text3.Text + "正确" + vbCrLf

        Else

            jl = Int(Rnd * 5 + 1)

            Select Case jl

            Case 1

                MsgBox "没有看清题目的要求?"

            Case 2

                MsgBox "需要提高注意力了!!!!"

            Case 3

                MsgBox "如此简单还要出错????"

            Case 4

                MsgBox "能不能仔细一些!!!!!!!"

            Case Else

                MsgBox "你是不是需要休息一下了?成绩太差了"

            End Select

            Text4.Text = Text4.Text + Text1.Text + "+" + Text3.Text + "=" + Text3.Text + "错误" + vbCrLf

        End If

    Case Else

        If Text3.Text = Val(Text1.Text) / Val(Text2.Text) Then

            jl = Int(Rnd * 5 + 1)   '选择5表示有五种可能的表扬方式

            Select Case jl

            Case 1

                MsgBox "今天发挥的不错"

            Case 2

                MsgBox "聪明的人,请继续努力"

            Case 3

                MsgBox "是你聪明还是运气不错"

            Case 4

                MsgBox "比较神奇的能力"

            Case Else

                MsgBox "判断相当的给力,继续努力"

            End Select

            Text4.Text = Text4.Text + Text1.Text + "+" + Text3.Text + "=" + Text3.Text + "正确" + vbCrLf

        Else

            jl = Int(Rnd * 5 + 1)

            Select Case jl

            Case 1

                MsgBox "没有看清题目的要求?"

            Case 2

                MsgBox "需要提高注意力了!!!!"

            Case 3

                MsgBox "如此简单还要出错????"

            Case 4

                MsgBox "能不能仔细一些!!!!!!!"

            Case Else

                MsgBox "你是不是需要休息一下了?成绩太差了"

            End Select

            Text4.Text = Text4.Text + Text1.Text + "+" + Text3.Text + "=" + Text3.Text + "错误" + vbCrLf

        End If

    End Select

End If

End Sub

 

Private Sub Command3_Click()

If Command3.Caption = "代码" Then

    Command3.Caption = "关闭"

    Text5.Visible = True

Else

    Command3.Caption = "代码"

    Text5.Visible = False

End If

End Sub

推荐阅读