VB语言编程
发布网友
发布时间:2022-04-26 15:21
我来回答
共3个回答
热心网友
时间:2022-05-01 19:20
Option Explicit
Private Sub Command1_Click()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Static r As Integer
Static ct As Integer
Static n As Integer
If Command1.Caption = "开始" Or Command1.Caption = "下一题" Then
a = Int(Rnd() * 90) + 10
b = Int(Rnd() * 90) + 10
c = Int(Rnd() * 4)
Select Case c
Case 0
Label2.Caption = a & " + " & b & " = ?"
r = a + b
Case 1
If a < b Then
r = a
a = b
b = r
End If
Label2.Caption = a & " - " & b & " = ?"
r = a - b
Case 2
a = Int(Rnd() * 100) + 1
b = Int(Rnd() * 100) + 1
If a < b Then
r = a
a = b
b = r
End If
r = a \ b
If r = 0 Then r = 1
a = b * r
a = r
Label2.Caption = a & " * " & b & " = ?"
r = a * b
Case 3
a = Int(Rnd() * 100) + 1
b = Int(Rnd() * 100) + 1
If a < b Then
r = a
a = b
b = r
End If
r = a \ b
If r = 0 Then r = 1
a = b * r
Label2.Caption = a & " / " & b & " = ?"
r = a \ b
End Select
Command1.Caption = "提交答案"
Label3.Caption = ""
Text1.Text = ""
Else
If Text1.Text = "" Then
MsgBox "请输入答案", vbCritical
Exit Sub
End If
Label2.Caption = Left(Label2.Caption, Len(Label2.Caption) - 1) & Text1.Text
n = n + 1
If Val(Text1.Text) = r Then
Label3.Caption = "√"
Label3.ForeColor = vbBlue
ct = ct + 1
Else
Label3.Caption = "×" & "(正确答案:" & r & ")"
Label3.ForeColor = vbRed
End If
List1.AddItem Label2.Caption & Label3.Caption
Command1.Caption = "下一题"
Label1.Caption = "正确率" & Format(ct / n, "00.0%")
End If
End Sub
Private Sub Form_Load()
Command1.Caption = "开始"
Label1.Caption = "正确率:0%"
Label2.Caption = "点开始按钮答题"
Label3.Caption = ""
Text1.Text = ""
Randomize Timer
End Sub
不明白的加百度HI给你发代码
热心网友
时间:2022-05-01 20:38
保存到1.frm,运行。。。代码里 设置题目总数 那行可以修改为需要的数字,希望能来得及,完。
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 6150
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 6150
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command2
Caption = "开始"
Height = 375
Left = 120
TabIndex = 4
Top = 2580
Width = 1095
End
Begin VB.CommandButton Command1
Caption = "提交"
Enabled = 0 'False
Height = 375
Left = 1080
TabIndex = 3
Top = 840
Width = 1095
End
Begin VB.ListBox List1
Height = 2760
Left = 3120
TabIndex = 2
Top = 120
Width = 2955
End
Begin VB.TextBox Text1
Height = 315
Left = 1740
TabIndex = 1
Top = 180
Width = 1155
End
Begin VB.Label Label1
Height = 315
Left = 180
TabIndex = 0
Top = 180
Width = 1275
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim A As Integer, B As Integer, C As Integer, NumDo As Integer, DoRight As Integer
Const NumAll As Integer = 10 '设置题目总数
Private Sub RandomNum() '生成算式
Randomize
C = Rnd() * 4
C = 3
If C = 0 Then
A = Int(Rnd() * 90) + 10
B = Int(Rnd() * 90) + 10
ElseIf C = 1 Then
A = 0
B = 99
Do While A < B
A = Int(Rnd() * 90) + 10
B = Int(Rnd() * 90) + 10
Loop
ElseIf C = 2 Then '不知道两位数乘法还是结果是两位数,按第二种吧
A = Int(Rnd() * 10)
B = Int(Rnd() * 10)
ElseIf C = 3 Then '同上,按九九乘法表+0+10的,结果可以整除的
B = Int(Rnd() * 10) + 1
A = B * Int(Rnd() * 10)
End If
If C = 0 Then '显示的,符号自己改
Label1.Caption = Trim(Str(NumDo)) & ". " & Trim(Str(A)) & " + " & Trim(Str(B)) & "="
ElseIf C = 1 Then
Label1.Caption = Trim(Str(NumDo)) & ". " & Trim(Str(A)) & " - " & Trim(Str(B)) & "="
ElseIf C = 2 Then
Label1.Caption = Trim(Str(NumDo)) & ". " & Trim(Str(A)) & " * " & Trim(Str(B)) & "="
ElseIf C = 3 Then
Label1.Caption = Trim(Str(NumDo)) & ". " & Trim(Str(A)) & " / " & Trim(Str(B)) & "="
End If
End Sub
Private Sub JiSuan() '计算得分
MsgBox "总分是" & Format(100 * (DoRight / NumAll), "00.0") & vbNewLine & "准确率" & Format((DoRight / NumAll) * 100, "00.00") & "%"
End Sub
Private Sub Command1_Click()
Dim D As Integer
If C = 0 Then
D = A + B
ElseIf C = 1 Then
D = A - B
ElseIf C = 2 Then
D = A * B
ElseIf C = 3 Then
D = A / B
End If
If Text1.Text = D Then
List1.AddItem Label1.Caption & D & " √"
DoRight = DoRight + 1
Else
List1.AddItem Label1.Caption & D & " ×"
End If
If NumDo < NumAll Then
NumDo = NumDo + 1
Call RandomNum
Else
Call JiSuan
Command1.Enabled = False
Command2.Caption = "重新开始"
End If
End Sub
Private Sub Command2_Click()
If Command2.Caption = "开始" Then
Command2.Caption = "重新开始"
Command1.Enabled = True
List1.Clear
NumDo = 1
DoRight = 0
Call RandomNum
Else
Command2.Caption = "开始"
Command1.Enabled = False
NumDo = 1
DoRight = 0
End If
End Sub
热心网友
时间:2022-05-01 22:12
希望对楼主有用:
Dim sum As Single
Private Sub Command1_Click()
Randomize
x = Int((101) * Rnd) (定义两位数!)
y = Int((101) * Rnd)
Select Case Int((4) * Rnd + 1)
Case 1
Text1.Text = x & "+" & y
sum = x + y
Case 2
Text1.Text = x & "-" & y
sum = x - y
Case 3
Text1.Text = x & "*" & y
sum = x * y
Case 4
Text1.Text = x & "/" & y
sum = x / y
End Select
End Sub
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 100
Label1.Caption = "欢迎使用算术练习题"
Label1.AutoSize = True
Label1.Move 0, 0
Command1.Caption = "出题"
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If sum = Val(Text2.Text) Then
List1.AddItem Text1.Text & "=" & Text2.Text & " √"
Else
List1.AddItem Text1.Text & "=" & Text2.Text & " X"
End If
End If
End Sub
Private Sub Timer1_Timer()
Label1.Left = Label1.Left + 20
If Label1.Left + Label1.Width >= Me.ScaleWidth Then Label1.Left = 0
End Sub