' 第一种图案 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行三角形
代码
1 2 3 4 5 6 7 8 9 10 11
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
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.平方逆序数
代码
1 2 3 4 5 6 7 8 9 10 11
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
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.累加阶乘
[alert]由于是手机截图说下要求:输入9显示409113[/alert]
代码
1 2 3 4 5 6 7 8 9 10 11 12
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
递归做法
1 2 3 4 5 6 7 8 9 10 11 12
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
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
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.直角三角形
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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.平方逆序数
代码
1 2 3 4 5 6 7 8 9
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.打印题
代码
1 2 3 4 5 6 7
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
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 这样的,用双重循环做会比较好一点
代码
1 2 3 4 5 6 7 8 9 10 11
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.打印图案
代码
1 2 3 4 5 6 7 8
Print For i = 1 To 5 Print Spc(5); For j = i To 5 Print "* "; Next Print Next
15.界面操作题
说明
文本框Alignment设置为属性2,拉三个复选按钮
代码
16.数组平均值
代码
1 2 3 4 5 6 7 8 9
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.输出图形
代码
1 2 3 4 5 6 7
For i = 1 To 5 Print "**"; For j = 1 To i Print i & "**"; Next Print Next
22.打印矩阵
代码
1
List1.AddItem Text1.Text
23.计算数组偶数和
代码
1 2 3 4 5 6 7 8 9
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.文本框复制删除
代码
1 2 3 4 5 6 7 8 9
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.循环体中的分支结构
代码
1 2 3 4 5 6 7
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.数组题
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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.平方和
代码
1 2 3 4
For i = 1 To 100 s = s + i ^ 2 Next Print s
30.斐波那契数列
代码
1 2 3 4 5 6 7 8 9 10
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
单输出
1 2 3 4 5 6 7 8
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,以此类推
代码
1 2 3 4 5 6 7 8
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.公式
(一元二次方程?)
代码
1 2 3 4 5 6 7 8 9 10
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*
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.打印题
代码
1 2 3 4 5 6 7 8 9 10 11 12
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
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.数组平均值
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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
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日:又错了,赣淦擀)
1 2 3 4 5 6 7 8 9 10 11 12
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
[collapse title=”错误代码”]
1 2 3 4 5 6 7 8 9 10 11 12
Private Sub Form_Click() Cls n = InputBox("n=") Sum n End Sub Function Sum(n) For i = 1 To Len(n) res = Val(Mid(n, i, 1)) s = s + res Next Print s End Function
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
[collapse title=”以前的代码”]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Public flag As Integer Private Sub Command1_Click() flag = flag + 1 If flag Mod 2 = 1 Then Form6.WindowState = 2 With Command1 .Left = (Me.Width - Command1.Width) / 2 .Top = (Me.Height - Command1.Height) / 2 End With Else Form6.WindowState = 0 Form6.Height = 3600 Form6.Width = 4800 With Command1 .Left = (Me.Width - Command1.Width) / 2 .Top = (Me.Height - Command1.Height) / 2 End With End If End Sub
[/collapse]
42.产生题目
代码
1 2 3 4 5 6 7
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
Private Sub Text1_Change() If IsNumeric(Text1) Then Text4 = (Text1) Else MsgBox "!" Exit Sub End If End Sub Private Sub Text2_Change() If IsNumeric(Text2) Then Text4 = (Text2) Else MsgBox "!" Exit Sub End If End Sub Private Sub Text3_Change() If IsNumeric(Text3) Then Text4 = (Text3) Else MsgBox "!" Exit Sub End If End Sub Private Sub Text4_Change() Text4 = (Val(Text1) + Val(Text2) + Val(Text3)) / 3 End Sub
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() If InStr(1, Text1, Text2) = False Then MsgBox "没有找到对应的字符串!" Else Text15 = Replace(Text1, Text2, Text3) End If End Sub Private Sub Form_Load() Text1 = "1" End Sub
46.列表框
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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
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.欢迎光临
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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
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.滚动条
代码
1 2 3 4 5 6 7
Private Sub Form_Load() w = Label1.Left HScroll1.Max = w End Sub Private Sub HScroll1_Change() Label1.Left = HScroll1.Value * 10 End Sub