VB综合技巧特训营(一)

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

统计字符个数

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Cls
Dim a(1 To 26), b(1 To 26)
Label1 = ""
For i = 1 To Len(Text1)
res = (Mid(Text1, i, 1))
If res >= "A" And res <= "Z" Then
j = Asc(res) - 66 + 2
a(j) = a(j) + 1
ElseIf res >= "a" And res <= "z" Then
l = Asc(res) - 97 + 1
b(l) = b(l) + 1
ElseIf res >= 0 And res <= 9 Then
n = n + 1
Else
k = k + 1
End If
Next
Label1 = Label1 & "Num: " & n & " " & "Other:" & k & vbCrLf
For i = 1 To 26
If a(i) > 0 Then
Label1 = Label1 & Chr(i + 64) & "=" & a(i) & " "
End If
Next
Label1 = Label1 & vbCrLf
For i = 1 To 26
If b(i) > 0 Then
Label1 = Label1 & Chr(i + 96) & "=" & b(i) & " "
End If
Next

d+dd+ddd

img

代码

(2021年3月2日,你妈的还不技能考)

之前那版本有问题,下面是新版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Cls
d = 123
n = 3
num = 0
For i = 1 To n
For j = 1 To i
t = t & CStr(d)
s = s + Int(t)
If n = i Then
Print Trim(t) & "=";
Else
Print Trim(t) & "+";
End If
If j = 1 Then Exit For
Next
Next
Print Trim(s);
' 下面是有问题的版本
Dim t As String, n As Long
d = Val(Text1)
n = Val(Text2)
For i = 1 To n
For j = 1 To i - j
t = t & d
s = s + Int(t)
If n = i Then
Print Trim(t) & "=";
Else
Print Trim(t) & "+";
End If
Next
Next
Print Trim(s);
Print

求平均分

img

新建工程,在标题为“计算得分”的窗体上有一个标题为“得分”的框架,在框架上有一个名称为Text1的文本框控件数组,含有6个文本框控件;文本框Text2用来输入难度系数,如图4-1。程序运行时,在左边的6个文本框中输入6个得分,输入难度系数后,单击“计算得分”按钮,则可以计算出最后得分,并显示在文本框Text3中,如图-1所示。

计算方法:

去掉1个最高分和1个最低分,求剩下得分的平均分,再乘以3,再乘以难度系数。最后结果保留到第2位小数,不四舍五入。

代码

1
2
3
4
5
6
7
8
9
10
11
12
Dim i, s As Double
Dim max, min As Double
max = Text1(0): min = Text1(0)
For i = Text1.LBound To Text1.UBound
s = s + Text1(i)
If Text1(i) > max Then max = Text1(i)
If Text1(i) < min Then min = Text1(i)
Next
Sum = ((s - max - min) / 4) * 3 * Val(Text7)
res = InStr(Sum, ".")
a = Val(Left(Sum, res + 2))
Text8 = a

处理数据

img

新建工程,程序的界面如图4-1所示。程序功能为通过键盘输入正整数到文本框Text1中。在“除数”框架中选择一个单选钮,然后单击“处理数据”按钮,将大于文本框中的正整数,并且能够被所选除数整除的5个数添加到列表框List1中。程序运行后,如图4-2所示。

[alert]没搞懂题目意思,不搞了[/alert]

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Dim n, s, i As Integer
List1.Clear
d = Val(Text1)
If Option1.Value = True Then n = 3
If Option2.Value = True Then n = 5
If Option3.Value = True Then n = 7
s = d * n
For i = 1 To s
If i Mod 5 = 0 Then
List1.AddItem i
n = n + 1
If n = 5 Then Exit For
End If
Next

统计字符

img

单击“产生数组”按钮时,随机生成20个0—10之间(不含0和10),并将其保存到一维数组a中,同时也将这20个数值在Text1文本框中显示。
单击“统计”按钮时,统计出数组a中出现频率最高的数值及其出现的次数,并将出现频率最高的数值在Text2文本框中显示、出现频率最高的次数在Text3文本框内显示。如图4-1所示。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Dim a(20), b(9)
Private Sub Command1_Click()
Text1 = ""
For i = 1 To 20
a(i) = Int(Rnd * (9 - 1 + 1) + 1)
n = n + 1
If n Mod 5 = 0 Then
Text1 = Text1 & a(i) & vbCrLf
Else
Text1 = Text1 & a(i) & " "
End If
Next
End Sub
Private Sub Command2_Click()
Text2 = ""
Text3 = ""
For i = 1 To 20
For j = 1 To 9
If a(i) = j Then
b(j) = b(j) + 1
Exit For
End If
Next
Next
max = 0
For i = 1 To 9
If b(i) > max Then
max = b(i)
End If
Next
For i = 1 To 9
If b(i) = max Then
Text2 = Text2 & i & " "
End If
Next
Text3 = max
End Sub

滚动条案例

img

新建工程,程序运行时,图片位于距离窗口顶部100的位置,滚动条的最小值为100,最大值为窗体内部高度;单击“开始”按钮,图片自上而下移动,同时滚动条的滑块随之移动。每隔0.5秒移动一次,每次移动200;当图片顶端移动到距离窗体下边界的距离少于200时,则再回到起始位置,重新向下移动,滚动条也从最小值开始移动。如图4-1所示。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Picture1.Top = 100
VScroll1.min = 100
VScroll1.max = Form1.Height
End Sub
Private Sub Timer1_Timer()
VScroll1.Value = VScroll1.Value + 200
Picture1.Top = VScroll1.Value
If Picture1.Top > Form1.Height - 200 Then
Picture1.Top = 0
VScroll1.Value = 100
End If
End Sub

通话计费

img

新建工程,程序的界面如图4-1所示。其中通过属性窗口对“通话结束”命令按钮的初始状态设置为不能用。程序功能是计算公用电话计时收费。计时收费的标准是:通话时间在3分钟以内时,收费0.5元;3分钟以上,每超1分钟加收0.15元,不足1分钟按1分钟计算。程序执行的操作如下:
如果单击“通话开始”按钮,则在“开始时间”右侧的文本框中显示开始时间,且“通话结束”按钮变为可用,“通话开始”按钮变为不可用。
如果单击“通话结束”按钮,则在“结束时间”右侧的文本框中显示结束时间,同时计算通话费用,并将其显示在“通话费用”右侧的文本框中,“通话开始”命令按钮变为可用,“通话结束”命令按钮变为不可用。如图4-2所示

不会吧不会吧,不会真有人乖乖等9分钟吧,不会吧不会吧

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Dim n As Integer
Private Sub Command1_Click()
Dim startTime, endTime As String
Text1 = time
Timer1.Enabled = True
Command1.Enabled = False
Command2.Enabled = True
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
sHour = hour(Text1)
sMinute = minute(Text1)
sSecond = second(Text1)
eHour = hour(Text2)
eMinute = minute(Text2)
eSecond = second(Text2)
cHour = eHour - sHour
cMinute = eMinute - sMinute
cSecond = eSecond - sSecond
If cMinute < 3 Then
Sum = 0.5
ElseIf cMinute >= 3 Then
ccMinute = cMinute - 3
Sum = 0.5 + (ccMinute * 0.15)
If cSecond < 60 Then
Sum = Sum + 0.15
End If
ElseIf cMinute < 1 Then
Sum = 0.15
End If
Text3 = Sum
Command1.Enabled = True
Command2.Enabled = False
End Sub
Private Sub Timer1_Timer()
Text2 = time
End Sub

相亲数

img

相亲数:任意一组整数,整数A和整数B,整数A的真因子和等于整数B,整数B的真因子和等于整数A,则称这一组整数为相亲数
e.g: 220 和 284

代码

行列转换

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Dim a(3, 2), b(2, 3) As Integer
Print "Before: "
For i = 1 To 3
For j = 1 To 2
n = n + 1
a(i, j) = n
Print a(i, j);
Next
Print
Next
Print "After: "
For i = 1 To 3
For j = 1 To 2
b(j, i) = a(i, j)
Next
Next
For i = 1 To 2
For j = 1 To 3
Print b(i, j);
Next
Print
Next

框架案例

imgimg

要求:所有按钮和复选框使用控件数组,将所选中的按钮值添加到Text1中

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Dim s As String
If Option1 = True Then
s = s + Option1.Caption
End If
If Option2 = True Then
s = s + Option2.Caption
End If
If Check1.Value = 1 Then
s = s + Check1.Caption
End If
If Check2.Value = 1 Then
s = s + Check2.Caption
End If
If Check3.Value = 1 Then
s = s + Check3.Caption
End If
Text1 = s

文字移动

img

要求:文字左右移动,上下移动

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Dim flag As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
Private Sub Option1_Click()
flag = 1
End Sub
Private Sub Option2_Click()
flag = 3
End Sub
Private Sub Timer1_Timer()
If flag = 1 Then
Label1.Left = Label1.Left + 50
If Label1.Left > Form1.Width - Label1.Left + 2800 Then
flag = 2
End If
ElseIf flag = 2 Then
Label1.Left = Label1.Left - 50
If Label1.Left < 0 Then
flag = 1
End If
End If
If flag = 3 Then
Label1.Top = Label1.Top + 50
If Label1.Top > Form1.Height - Label1.Height - 300 Then
flag = 4
End If
ElseIf flag = 4 Then
Label1.Top = Label1.Top - 50
If Label1.Top < 0 Then
flag = 3
End If
End If
If Check1.Value = 1 Then
If Label1.Left < 0 Or Label1.Left > Form1.Width - Label1.Left + 2800 Or _
Label1.Top < 0 Or Label1.Top > Form1.Height - Label1.Height - 300 Then
Timer1.Enabled = False
End If
End If
End Sub

统计个数

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
For i = 100 To 999
a = i Mod 10
b = i \ 10 Mod 10
c = i \ 100 Mod 10
If c = a * b Then
Print i;
s = s + i
n = n + 1
If n Mod 3 = 0 Then Print
End If
Next
Print
Print "满足三位数中的百位数与个位和十位乘积的个数:" & n
Print "这" & n & "个数和:" & s

相邻素数

img img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Private Sub Command1_Click()
Cls
Dim y As Integer, k As Integer, m As Integer
y = Text1
k = y + 1
m = y - 1
Do
If Not prime(k) Then
k = k + 1
End If
Loop Until prime(k)
Do
If Not prime(m) Then
m = m - 1
End If
Loop Until prime(m)
If k - y < y - m Then
Print k
ElseIf k - y > y - m Then
Print m
Else
Print m & "与" & k
End If
End Sub
Private Function prime(n As Integer) As Boolean
prime = True
For i = 2 To Sqr(n)
If n Mod i = 0 Then
prime = False
Exit Function
End If
Next
End Function

字符转换

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
before = "abcdefgABCDEFG123456 wdnmd"
Print (before);
For i = 1 To Len(before)
res = Mid(before, i, 1)
If res Like "[*A-Z]" Then
after = after & LCase(res)
ElseIf res Like "[*a-z]" Then
after = after & UCase(res)
ElseIf res = " " Then
after = after & " "
Else
after = after & "*"
End If
Next
Print
Print after

限制聊天

img

如GIF

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Public n, l, z As Integer
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Print "你说:" & Text1
n = n + 1
If n = 5 Then
n = 0
z = z + 1
MsgBox "发言过快,禁言3秒中"
Text1.Enabled = False
Text1.Text = "禁言中"
Text1.Alignment = 2
Timer1.Enabled = True
ElseIf z = 3 Then
MsgBox "永久封禁", vbExclamation
Text1.Enabled = False
Text1.Text = "该账号已被永久封禁"
Text1.Alignment = 2
End If
End If
End Sub
Private Sub Timer1_Timer()
l = l + 1
If l = 3 Then
l = 0
Text1 = ""
Text1.Enabled = True
Text1.Alignment = 0
Timer1.Enabled = False
End If
End Sub

数组操作

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Dim a(1 To 10)
Public n As Integer
Private Sub Command1_Click()
Text1 = ""
Command1.Enabled = False
Command2.Enabled = True
Text1 = a(LBound(a))
End Sub
Private Sub Command2_Click()
Text1 = ""
Command1.Enabled = True
Command2.Enabled = False
Text1 = a(UBound(a))
End Sub
Private Sub Command3_Click()
n = n - 1
Print n
If n = 1 Then
Command3.Enabled = False
Command4.Enabled = True
End If
Text1 = a(n)
End Sub
Private Sub Command4_Click()
n = n + 1
Print n
If n >= 10 Then
n = 10
Command4.Enabled = False
Command3.Enabled = True
Exit Sub
End If
Text1 = a(n)
End Sub
Private Sub Form_Load()
Command3.Enabled = False
a(1) = "中国好"
a(2) = "中国好好"
a(3) = "富强民主文明和谐"
a(4) = "自由平等公正法制"
a(5) = "爱国敬业诚信友善"
a(6) = "中国特色社会主义"
a(7) = "社会主义核心价值观"
a(8) = "激情红色!青春光彩!"
a(9) = "第九套全国小学生广播体操"
a(10) = "七彩阳光,现在开始!"
End Sub

二分查找

img

久违的算法题:这里in3.dat文件就不给了,我直接把数据放在下面,你们自己声明个数组加进去

1
1,9,12,23,24,35,38,43,45,50,52,53,57,58,59,61,62,64,65,66,68,69,72,75,78,85,89,92,95,96,122,153,203,222,231,232,235,236,237,245,256,300,301,304,305,322,345,362,368,370,371,375,379,384,395,400,407,416,425,430,444,450,452,460,465,493,525,526,560,567,580,586,587,595,599,600,625,634,679,687,688,693698,723,729,738,750,751,754,791,801,806,823,843,872,890,893,895,956,962

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Dim a() As String
Dim mid As String
Dim item As Integer
Private Sub Command1_Click()
t = "1,9,12,23,24,35,38,43,45,50,52,53,57,58,59,61,62,64,65,66,68,69,72,75,78,85,89,92,95,96,122,153,203,222,231,232,235,236,237,245,256,300,301,304,305,322,345,362,368,370,371,375,379,384,395,400,407,416,425,430,444,450,452,460,465,493,525,526,560,567,580,586,587,595,599,600,625,634,679,687,688,693698,723,729,738,750,751,754,791,801,806,823,843,872,890,893,895,956,962"
item = Val(InputBox("请输入要查找的元素:"))
a = Split(t, ",")
low = 0
high = UBound(a)
Do While low <= high
mid = (low + high)
guess = a(mid)
If guess = item Then
Text2 = "查找成功,该数的位置为 " & mid + 1 & "!"
End If
If guess > item Then
high = mid - 1
Else
low = mid + 1
End If
Loop
End Sub

书本上的

二级原题作答

打印题

img

这题来源一个文考516的大佬,膜了

代码

1
2
3
4
5
6
7
8
9
10
For i = 1 To 9
Print i;
Next
Print
For i = 1 To 9
For j = 1 To i
Print j * i;
Next
Print
Next

统计最长单词

img

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Cls
s = Text1
Max = 0
For i = 1 To Len(s)
res = Mid(s, i, 1)
If res = " " Then
n = 0
Else
n = n + 1
End If
If n > Max Then
Max = n
strRes = Mid(s, i - n + 1, n)
End If
Next
Text2 = Trim(Str(Max))
Text3 = Trim(strRes)

相邻数的最值

img

img

img

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Option Base 1
Dim a(10)
Private Sub Form_Load()
Cls
Show
Label1 = ""
For i = 1 To 10
a(i) = Int(Rnd * (99 - 10 + 1)) + 10
Label1 = Label1 & a(i) & " "
Next
End Sub
Private Sub Text1_Change()
Cls
Dim max, min As Integer
Dim le, re, left, right As Integer
Print
search = Text1
If search >= 1 And search <= 10 Then
Print Space(search * 3 - 3) & String(1, "I")
If search = 1 Then
left = 0
right = a(search + 1)
Print "您找的数字是:" & a(search)
Print "它左边的数字是:" & 0
Print "它右边的数字是:" & a(search + 1)
Print "位置:" & search
ElseIf search = 10 Then
left = a(search - 1)
right = 0
Print "您找的数字是:" & a(search)
Print "它左边的数字是:" & a(search - 1)
Print "它右边的数字是:" & 0
Print "位置:" & search
Else
left = a(search - 1)
right = a(search + 1)
Print "您找的数字是:" & a(search)
Print "它左边的数字是:" & a(search - 1)
Print "它右边的数字是:" & a(search + 1)
Print "位置:" & search
End If
End If
max = left: min = left
If left > right Then
max = left
Else
max = right
End If
If left < right Then
min = left
Else
min = right
End If
Print "最大的数是:" & max
Print "最小的数是:" & min
End Sub

移动列表项

img

代码

计算名次

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Dim sum, max As Integer
Dim a(10, 5)
Print "成绩 1 成绩 2 成绩3 总分 名次"
For i = 1 To 10
sum = 0
For j = 1 To 4
If j > 0 And j < 4 Then
a(i, j) = Int(Rnd * (99 - 10 + 1)) + 10
sum = sum + a(i, j)
End If
Next
a(i, 4) = sum
Next
For i = 1 To 10
max = a(i, 4)
For j = i + 1 To 10
If max < a(j, 4) Then
For l = 1 To 4
max = a(j, l)
a(j, l) = a(i, l)
a(i, l) = max
Next
End If
Next
Next
For i = 1 To 10
For j = 1 To 5
Print Space(2) & a(i, j) & Space(3);
If j = 4 Then
Print i;
End If
Next
Print
Next

完数和他的因数们

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Cls
Dim i, s As Integer
For i = 1 To 1000
s = 0
For j = 1 To i - 1
If i Mod j = 0 Then
s = s + j
End If
Next
If s = i Then
mystr = Str(i) + " 的因数是:"
For a = 1 To s - 1
If s Mod a = 0 Then
mystr = mystr + Str(a) + ";"
End If
Next
Print mystr;
Print
End If
Next

百钱买百鸡

img

你有100元,要求买100只鸡,其中小鸡每只5角,公鸡每只2元,母鸡每只3元,列出所有满足百钱百鸡的条件

1
2
3
4
5
6
7
8
9
10
11
12
13
Print "母鸡", "公鸡", "小鸡"
For i = 0 To 33
' 母鸡每只3元,所以3*33=99不能超过100
For j = 0 To 50
' 公鸡每只2元,一样不能超过100
If 3 * i + 2 * j + 0.5 * (100 - i - j) = 100 Then
' 如果母鸡+公鸡+小鸡的组合刚好等于100则输出
' 小鸡 0.5 * (100-i-j)说明
' 小鸡一只0.5元,每次枚举的时候需要减去公鸡和母鸡的个数,这样才能达到平衡
Print i, j, 100 - i - j
End If
Next
Next

判断闰年

img

判断输入的年份是否为闰年,闰年必须满足以下条件:1、可以被400整除,2、可以被4整除,但是不能被100整除

1
2
3
4
5
6
7
Dim year As Integer
year = Int(Text1.Text)
If year Mod 400 = 0 Or (year Mod 4 = 0 And year Mod 100 <> 0) Then
MsgBox year & "是闰年"
Else
MsgBox year & "不是闰年"
End If
Author

IceCliffs

Posted on

2021-02-18

Updated on

2024-12-03

Licensed under

Comments