VB语言程序设计上机操作五十道题目及答案

这真的很重要!关于题目版权声明及部分事项 👈 戳它进行查看

说明

所有题目均来自VBForum、微信群、TG群、福建省高校、QQ群、百度贴吧及各大互联网,出题者保留所有权利,本人仅作解题作答及题目整理,你们出事了别赖我哈,禁止商用(商用禁止)

1.打印题(S1)

代码

连在一起做的,自己看序号

' 第一种图案
For i = 5 To 1 Step -1
     Print Space(i);
     For j = i To 5
          Print Trim(j);
     Next
     For j = 4 To i Step -1
          Print Trim(j);
     Next
     Print
Next
' 第二种图案
For i = 1 To 5
     Print Space(i);
     For j = i To 5
          Print Trim(j);
     Next
     For j = 4 To i Step -1
          Print Trim(j);
     Next
     Print
Next
' 第四种图案
For i = 9 To 6 Step -1
     Print Spc(i - 6);
     For j = 9 To i Step -1
          Print Trim(j);
     Next
     For j = i To 9 - 1
          Print Trim(j + 1);
     Next
     Print
Next
For i = 7 To 9
     Print Space(i - 6);
     For j = 9 To i Step -1
          Print Trim(j); '
     Next
     For j = i + 1 To 9
          Print Trim(j); '
     Next
     Print
Next
' 第五种图案
For i = 9 To 7 Step -1
     Print Spc(i - 6);
     For j = i To 9
          Print Trim(j);
     Next
     For j = 8 To i Step -1
          Print Trim(j);
     Next
     Print
Next
For i = 6 To 9
     Print Spc(i - 6);
          For j = i To 9
          Print Trim(j);
     Next
     For j = 8 To i Step -1
          Print Trim(j);
     Next
     Print
Next

2.打印n行三角形

代码

n = Val(InputBox("请输入N:", "vb5"))
For i = 1 To n
     Print Tab(n * 2 - i);
     For j = 1 To i
          Print "*";
     Next
     For j = i - 1 To 1 Step -1
          Print "*";
     Next
     Print
Next

3.求最大公约数

关于最大公约数的解法可以查看本站另一篇文章👉最大公约数和最小公倍数

代码

Me.FontSize = 15
Cls
If Text1 > Text2 Then
     t = Text1
     Text1 = Text2
     Text2 = t
End If
For i = 1 To Text2
     If Text1 Mod i = 0 And Text2 Mod i = 0 Then
          ss = Text1.Text & " 和 " & Text2.Text & " 最大公约数是:" & i
     End If
Next
Print ss

4.平方逆序数

代码

For i = 10 To 99
     If i <> StrReverse(i) Then
          a = Mid(i, 1, 1)
          b = Mid(i, 2, 1)
          s = Val(Str(a + b) ^ 2)
          If a <= b And s = StrReverse(StrReverse(i) ^ 2) Then
               Print i & "^2=" & s, _
               StrReverse(i) & "^2="; StrReverse(s)
          End If
     End If
Next

5.产生数列(二级)

文本框text4,Multinue记得设置为True

代码

(2021年3月2日补充,这题总算搞懂了)

Text4 = ""
a = Val(Text1)
b = Val(Text2)
n = Val(Text3)
Text4 = Text4 & a & " " & b
r = 2
Do While r < n
     c = a * b
     r = r + 1
     If c < 10 Then
          Text4 = Text4 & Space(1) & c
          a = b
          b = c
     Else
          d = c \ 10
          Text4 = Text4 & " " & d
          a = d
          r = r + 1
          If r <= n Then
               d = c Mod 10
               Text4 = Text4 & " " & d
               b = d
          End If
     End If
Loop
Text4 = ""
Dim s As Integer
a = Val(Text1)
b = Val(Text2)
c = Val(Text3)
n = 2
If a >= 1 And a <= 10 Or b >= 1 And b <= 10 Then
     Text4 = Text4 & a & Space(1) & b
     For i = 1 To c
          s = a * b
          If s >= 10 Then
              a = Mid(s, 1, 1)
              b = Mid(s, 2, 1)
              Text4 = Text4 & Space(1) & a
              n = n + 1
              If n >= Val(c) Then
                   Exit For
              End If
              Text4 = Text4 & Space(1) & b
          End If
          If s < 10 Then
              Text4 = Text4 & Space(1) & s
                   a = b
                   b = s
                   n = n + 1
          End If
     Next
End If

6.累加阶乘

由于是手机截图说下要求:输入9显示409113

代码

Public n As Integer
Private Sub Command1_Click()
     n = Val(InputBox("输入数据", "工程1"))
End Sub
Private Sub Command2_Click()
m = 1
For i = 1 To n
     m = m * i
     s = s + m
Next
Print s
End Sub

递归做法

Function CountNum(n As Integer)
If n > 0 Then
     s = 1
     For i = 1 To n
          s = s * i
     Next
     CountNum = CountNum(n - 1) + s
End If
End Function
Private Sub Command1_Click()
     Print CountNum(9)
End Sub

7.打印题(S2)

代码

For i = 1 To 5
     For j = 1 To 5
          t = i + j - 1
          If t > 5 Then
               t = t - 5
          End If
          Print t;
     Next
     Print
Next
n = 6
For i = 1 To n
     For j = 1 To n
     If i <= j Then
          Print j;
          If j = n And i <> 1 Then
               For l = 1 To n - 1
                    If i - 1 >= l Then
                         Print l;
                    End If
               Next
          End If
     End If
     Next
     Print
Next

8.空心等腰三角形

代码

For i = 1 To 25
     If i = 1 Then Print Space(24) & "*";
         If i > 1 And i <= 24 Then
              Print Space(25 - i) & "*" & Space(i * 2 - 3) & "*";
         End If
     If i = 25 Then Print String(i * 2 - 1, "*");
Print
Next
n = 5
For i = 1 To n
     Print Space(n - i);
     For j = 1 To n
          If i >= j And i = j Then
               Print "*";
               If i >= 2 And i + 1 = j + 1 Then
                    Print Space(i - 1 + (j - 2)); "*";
                    Exit For
               End If
          End If
          If i = n Then
               Print String(i + i - 1, "*");
               Exit For
          End If
     Next
     Print
Next

9.直角三角形

代码

n = 10
For i = 1 To n
     Print Spc(n - i);
     For j = 1 To i
          If i >= j And j = i Then
               Print "*";
               If j >= 2 Then
                    Print Spc(i - 2); "*";
               End If
          End If
          If i = n Then
               Print String(i, "*");
               Exit For
          End If
     Next
     Print
Next

10.平方逆序数

代码

For i = 1 To 3
     Print Spc(27 - 3 * i);
     Print Spc(9 - 3 * i);
     For j = 1 To 2 * i - 1
          s = s + 1
          Print s;
     Next
     Print
Next

11.打印题

代码

For i = 1 To 5
     Print Spc((5 - i) * 6 + 1);
     For j = 1 To i
          Print Format(String(j, CStr(i)), String(6, "@"));
     Next
     Print
Next

12.打印矩阵

代码

Option Base 1
Private Sub Form_Click()
Cls
Dim a(5, 5)
Print
Print "初始矩阵为:"
Print
For i = 1 To 5
     Print Tab(3);
     For j = 1 To 5
          a(i, j) = Int(Rnd * (99 - 10 + 1)) + 10
          Print a(i, j) & Space(2);
     Next
Next
Print
Print
Print "右上 - 左下对角线上的数为:"
Print
For i = 1 To 5
     For j = 1 To 5
          If j = 6 - i Then
               Print a(i, j);
          End If
     Next
Next
End Sub

 

13.求连续和

说明

所谓连续和就是1+2+3…+4+5 这样的,用双重循环做会比较好一点

代码

Print "连续和为1250的正整数是:"
For i = 1 To 1249
     For j = i To 1249
          s = s + j
          If s = 1250 Then
               Print Spc(2);
               Print i & " ~ " & j
          End If
     Next
     s = 0
Next

14.打印图案

代码

Print
For i = 1 To 5
     Print Spc(5);
     For j = i To 5
          Print "* ";
     Next
     Print
Next

15.界面操作题

说明

文本框Alignment设置为属性2,拉三个复选按钮

代码

16.数组平均值

代码

Dim a(20)
For i = 1 To 20
     Randomize
     a(i) = Int(Rnd * (1000 - 0 + 1)) + 0
     s = s + a(i)
     Print a(i);
Next
Print
Print s / 20

17.求阶乘

代码

18.计算成绩

代码

19.选课程

代码

20.输入密码进行验证

代码

21.输出图形

代码

For i = 1 To 5
     Print "**";
     For j = 1 To i
          Print i & "**";
     Next
     Print
Next

22.打印矩阵

代码

List1.AddItem Text1.Text

23.计算数组偶数和

代码

Dim a(10) As Integer
For i = 1 To 10
     a(i) = Int(Rnd * (100 - 0 + 1)) + 0
     Print a(i);
     If a(i) Mod 2 = 0 Then
          s = s + a(i)
     End If
Next
MsgBox "偶数和" & s

24.文本框复制删除

代码

Private Sub Command1_Click()
Text2 = Text1.SelText
End Sub
Private Sub Command2_Click()
Text1.SelText = ""
End Sub
Private Sub Form_Load()
Text1 = "比炉灶之火尚还赤红熊熊燃揉啊?揉啊?揉啊?揉啊揉啊揉啊!(嗨)想看更豪华的料理~?没辙啊~真是的~(秘方什么的不~给看不~给看不~给看)(厨房里面也不~给看不~给看不~给看"
End Sub

25.列表框管理

代码

26.查找最大值(三个数)

代码

27.循环体中的分支结构

代码

For i = 1 To 100
     If i Mod 3 = 0 Or i Mod 5 = 0 Then
          Text1 = Text1 & i & Space(1)
          s = s + i
     End If
Next
Text2 = s

28.数组题

代码

Dim a(1 To 10) As Integer
For i = 1 To 10
     a(i) = Val(InputBox("请输入数组,这是第" & i & "个", i))
     Print a(i);
Next
Max = a(1)
For i = 1 To 10
     If a(i) > Max Then
          Max = a(i)
          gap = i
     End If
Next
Print
Print "Max: " & Max & " Gap: " & gap

29.平方和

代码

For i = 1 To 100
     s = s + i ^ 2
Next
Print s

30.斐波那契数列

代码

Text1 = ""
Dim a(50) As Long
a(1) = 1
a(2) = 1
N = 40
For i = 3 To N
     s = a(i - 2) + a(i - 1) + 1
     a(i) = s
Next
Text1 = s

单输出

Dim a(40)
a(1) = 1
a(2) = 1
Text1 = Text1 & a(1) & Space(1) & a(2) & Space(1)
For i = 3 To 40
     a(i) = a(i - 2) + a(i - 1) + 1
     Text1 = Text1 & a(i) & Space(1)
Next

31.斐波那契数列

1,1,2,3,5,8,从第三位开始前两项的和,1+1=2,2+3=5,以此类推

代码

Dim a(40)
a(1) = 1
a(2) = 1
Text1 = Text1 & a(1) & Space(1) & a(2) & Space(1)
For i = 3 To 40
     a(i) = a(i - 1) + a(i - 2)
     Text1 = Text1 & a(i) & Space(1)
Next

递归做法

 

32.公式

(一元二次方程?)

代码

Cls
x = Val(InputBox("请输入x值"))
If x < -5 Then
     y = 3 * x ^ 2 + 2 * x - 1
ElseIf x >= -5 And x <= 5 Then
     y = x * Sin(x * 3.14 / 180) + 2 ^ 2
ElseIf x > 5 Then
     y = Sqr(x - 5) + Log(x) / Log(10)
End If
Print "y= " & y*

33.判断素数(质数)

质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。

e.g:

1=1

2=1,2(质数)

3=1,3(质数)

4=1,2,4

5=1,5(质数)

102=1,2,3,6,17,34,51,102

代码

34.列表框综合应用

代码

Private Sub Command1_Click()
Cls
For i = 0 To List1.ListCount - 1
     If List1.Selected(i) = True Then
          Print "您选择了" & List1.List(i)
     End If
Next
If List1.ListIndex = -1 Then
     MsgBox "请选择!"
     Exit Sub
End If
End Sub
Private Sub Command2_Click()
If List1.ListIndex = -1 Then
     MsgBox "请选择!"
     Exit Sub
End If
For i = 0 To List1.ListCount - 1
     If List1.List(List1.ListIndex) = List2.List(i) Then
          MsgBox "该项已添加"
          Exit Sub
     End If
Next
List2.AddItem List1.List(List1.ListIndex)
List1.RemoveItem (List1.ListIndex)
End Sub
Private Sub Command3_Click()
For i = 0 To List1.ListCount - 1
     List2.AddItem List1.List(i)
Next
List1.Clear
End Sub
Private Sub Command4_Click()
If List2.ListIndex = -1 Then
     MsgBox "请选择"
     Exit Sub
End If
For i = 0 To List2.ListCount - 1
     If List2.List(List2.ListIndex) = List1.List(i) Then
     MsgBox "该项已添加"
     Exit Sub
End If
Next
List1.AddItem List2.List(List2.ListIndex)
End Sub
Private Sub Command5_Click()
For i = 0 To List2.ListCount - 1
     List1.AddItem List2.List(i)
Next
List2.Clear
End Sub
Private Sub Command6_Click()
If List1.ListIndex = -1 Then
     MsgBox "请选择"
     Exit Sub
End If
List1.RemoveItem List1.ListIndex
End Sub
Private Sub Command7_Click()
If List2.ListIndex = -1 Then
     MsgBox "请选择"
     Exit Sub
End If
List2.RemoveItem List2.ListIndex
End Sub
Private Sub Command9_Click()
List1.AddItem Text1.Text
For i = 0 To List1.ListCount - 1
     If Text1.Text = List1.List(i - 1) Then
          MsgBox "禁止重复添加"
          Exit Sub
     End If
Next
End Sub
Private Sub Form_Load()
List1.AddItem "Xi jinping"
List1.AddItem "Mao zedong"
List1.AddItem "Donald J Trump"
List1.AddItem "Joe Biden"
List1.AddItem "OYang"
End Sub

35.打印题

代码

36.打印题

代码

Dim a(5, 5) As Integer
For i = 1 To 5
     For j = 1 To 5
          If i <= j Then
               a(i, j) = 1
          Else
               a(i, j) = 0
          End If
     Print a(i, j);
     Next
     Print
Next

37.数组查最大值

代码

Cls
Dim a(5, 5) As Integer
For i = 1 To 5
     For j = 1 To 5
          a(i, j) = Int(Rnd * (99 - 10 + 1)) + 10
          Print a(i, j);
     Next
     Print
Next
Max = a(1, 1)
For i = 1 To 5
     For j = 1 To 5
          If a(i, j) > Max Then
               Max = a(i, j)
               x = i
               y = j
          End If
     Next
Next
Print "最大元素为 " & Max & " 位于第 " & x & " 行 " & y & " 列"

38.数组平均值

代码

Private Sub Form_Click()
n = CDbl(InputBox("请输入数组元素的个数:", "数组长度"))
Average n
End Sub
Function Average(n)
Cls
Dim a() As Double
ReDim a(n) As Double
For i = 1 To n
     a(i) = CDbl(Rnd * (99 - 10 + 1)) + 10
     s = s + a(i)
Next
Print s / n
End Function

 

39.打印矩阵

代码

(2021年4月1日:这题我写错了,下面是新的代码)

Private Sub Form_Click()
Cls
Dim n(1 To 6)
For i = 1 To 6
     n(i) = Int(Rnd * (99 - 10 + 1)) + 10
     Print n(i);
Next
Print
Sort n()
For i = 1 To 6
     Print n(i);
Next
End Sub
Sub Sort(n())
For i = 1 To UBound(n)
     For j = 1 To UBound(n) - 1
          If n(j) > n(j + 1) Then
               t = n(j)
               n(j) = n(j + 1)
               n(j + 1) = t
          End If
     Next
Next
End Sub

40.各个数字之和

代码

(2021年4月1日:又错了,赣淦擀)

Private Sub Command21_Click()
Sum (123)
End Sub

Function Sum(n) As Integer
Dim s As Integer
n = Abs(n)
For i = 1 To Len(n)
     s = s + Mid(n, i, 1)
Next
Print s
End Function

 

 

41.窗体练习

按钮的Caption属性值设置为:最大化(&L) (既可以显示下划线)

代码

(2021年4月1日:天哪,你怎么错得那么多)

Private Sub Command1_Click()
Static i As Integer
i = i + 1
If i Mod 2 = 0 Then
     Command1.Caption = "最大化(&L)"
     Form1.WindowState = 2
     Command1.Left = (Me.Width - Command1.Width) \ 2
     Command1.Top = (Me.Height - Command1.Height) \ 2
Else
     Command1.Caption = "最小化(&B)"
     Form1.WindowState = 0
     Command1.Left = (Me.Width - Command1.Width) \ 2
     Command1.Top = (Me.Height - Command1.Height) \ 2
End If
End Sub

42.产生题目

代码

Private Sub Command1_Click()
Text1.FontSize = 15
Dim x1, x2 As Integer
x1 = Int(Rnd * (100))
x2 = Int(Rnd * (100))
Text1 = Text1 & x1 & "+" & x2 & "=( )" & vbCrLf
End Sub

43.计算平均值

代码

平均成绩的文本框属性修改成 Locked=True ,这里我推荐使用控件数组来做,不会的可以问少雷老师,这里就不多讲了(笑)
l = Text1.UBound
For i = 0 To l
     If IsNumeric(Val(Text1(i))) Then
          s = s + Val(Text1(i))
     End If
Next
Text12 = s / (l + 1)

44.秒表模拟

format大法好(虽然这题考的是datediff的运用),虽然做法不是我的

代码

Private Sub Command25_Click()
Text9 = Time
End Sub
Private Sub Command26_Click()
Text10 = Time
Text12 = Format(DateDiff("hms", Text9, Text10), "00:00:00")
End Sub

投机取巧法

Private Sub Command1_Click()
Text1 = Time
End Sub

Private Sub Command2_Click()
Text2 = Time
h1 = Hour(Text1)
m1 = Minute(Text1)
s1 = Second(Text1)
h2 = Hour(Text2)
m2 = Minute(Text2)
s2 = Second(Text2)
Text12 = h2 - h1 & ":" & m2 - m1 & ":" & s2 - s1
End Sub

 

45.替换字符串

代码

Private Sub Command1_Click()
If InStr(1, Text1, Text2) = False Then
     MsgBox "没有找到对应的字符串!"
Else
     Text15 = Replace(Text1, Text2, Text3)
End If
End Sub
Private Sub Form_Load()
Text1 = "中华人民共和国中国共产党"
End Sub

46.列表框

代码

Private Sub Command1_Click()
If Text1 = "" Then
     MsgBox "没有内容,不予添加", , 3
Else
     List1.AddItem Text1
     Text1.SetFocus
End If
End Sub
Private Sub Command2_Click()
If List1.ListIndex = -1 Then
     MsgBox "请选择输出的项目", , 3
Else
     List1.RemoveItem List1.ListIndex
End If
End Sub
Private Sub Command3_Click()
List1.Clear
Text1 = ""
End Sub

47.组合框运用

代码

Private Sub Combo1_Click()
Select Case Combo1.ListIndex
     Case 0
          Text1.Alignment = 0
     Case 1
          Text1.Alignment = 2
     Case 2
          Text1.Alignment = 1
End Select
End Sub
Private Sub Combo2_Click()
Select Case Combo2.ListIndex
     Case 0
          Text1.FontItalic = False
          Text1.FontBold = False
     Case 1
          Text1.FontItalic = True
          Text1.FontBold = False
     Case 2
          Text1.FontBold = True
          Text1.FontItalic = False
     Case 3
          Text1.FontItalic = True
          Text1.FontBold = True
End Select
End Sub
Private Sub Form_Load()
Combo1.AddItem "左对齐"
Combo1.AddItem "居中"
Combo1.AddItem "右对齐"
Combo2.AddItem "常规"
Combo2.AddItem "斜体"
Combo2.AddItem "粗体"
Combo2.AddItem "粗斜体"
End Sub

48.欢迎光临

代码

Public flag As Integer
Private Sub Command1_Click()
flag = flag + 1
If flag Mod 2 = 1 Then
     Timer1.Enabled = True
     Command1.Caption = "停止"
Else
     Timer1.Enabled = False
     Command1.Caption = "开始"
End If
End Sub
Private Sub Timer1_Timer()
Label1.FontSize = Label1.FontSize + 2
End Sub

49.计时器

代码

Public flag As Integer
Private Sub Command1_Click()
flag = flag + 1
If flag Mod 2 = 1 Then
     Timer1.Enabled = True
     Command1.Caption = "停止"
Else
     Timer1.Enabled = False
     Command1.Caption = "开始"
End If
End Sub
Private Sub Form_Load()
With Label1
.Font = "宋体"
.FontBold = True
.FontSize = 24
.ForeColor = vbRed
.Left = (Me.Width - .Width) / 2
.Top = (Me.Height - .Height) / 2
.Alignment = 2
End With
End Sub
Private Sub Timer1_Timer()
Static i As Integer
i = i + 1
If i Mod 2 = 0 Then
     Label1.ForeColor = vbRed
Else
     Label1.ForeColor = vbBlue
End If
End Sub

 

50.滚动条

代码

Private Sub Form_Load()
w = Label1.Left
HScroll1.Max = w
End Sub
Private Sub HScroll1_Change()
Label1.Left = HScroll1.Value * 10
End Sub
文章《VB语言程序设计上机操作五十道题目及答案》采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: www.bilibili.com
Source: ななひら
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
嘉然
ななひら
小恐龙
花!
上一篇
下一篇