VB编程程序代码
1、求100以内的素
Private Sub Form_Click<>
Dim i%, j%
For i = 2 To 100
For j = 2 To i - 1
If i Mod j = 0 Then Exit For
Next j
If j = i Then Print i
Next i
End Sub
2、从键盘输入任意长度的字符串,要求将字符顺序倒置例如,将输入的"ABCDEFG"变换成"GFEDCBA". Private Sub Command1_Click<>
Dim a$, I%, c$, d$, n%
a = InputBox$<"输入字符串">
n = Len<a>
For I = 1 To Int<n \ 2>
c = Mid<a, I, 1>
Mid<a, I, 1> = Mid<a, n - I + 1, 1>
Mid<a, n - I + 1, 1> = c
Next I
Print a
End Sub
3、计算0~200之间所有能被11或5整除的数之和Private Sub Form_Click<>
Dim n%, i%
n = 0
For i = 1 To 200
If i Mod 11 = 0 Or i Mod 5 = 0 Then
n = n + i
End If
Next i
Print n
End Sub
4、输入一年份,判断它是否为闰年,并显示有关信息.
〔判断闰年的条件是:年份能被4整除但不能被100整除,或者能被400整除〕Private Sub Command1_Click<>
Dim y%
y = InputBox<"请输入年数">
If y Mod 4 = 0 And y Mod 100 <> 0 Or y Mod 400 = 0 Then
MsgBox <y & "年是闰年">
Else
MsgBox <y & "年是平年">
End If
End Sub
5、已知x,y,z 3个变量中存放了3个不同的数,比较它们的大小并进行调整,使得x<y<z. Private Sub Command1_Click<>
Dim x!, y!, z!简单的vb程序代码
x = InputBox<"inputx">
y = InputBox<"inputy">
z = InputBox<"inputz">
Print "      x      y    z"
If x > y Then t = x: x = y: y = t
If x > z Then t = x: x = z: z = t
If y > z Then t = y: y = z: z = t
Print "排序后"; x; "  "; y; "  "; z
End Sub
6、求s=a+aa+aaa+...aaaaa〔n个a〕,其中a和n的值随机产生,a的范围是[1,9]的整数,n的范围是[如a=3,n=6,则s=3+33+333+3333+33333+333333.编程确定n和a的值,并计算s.
Private Sub Form_Click<>
Dim s!, t!, i!, a%, n%
a = Int<Rnd * 9 + 1>
n = Int<Rnd * 6 + 5>
t = 0: s = 0
Print "a="; a, "n="; n
For i = 1 To n
t = t * 10 + a
s = s + t
Print t;
Next i
Print
Print "s="; s
End Sub
7、计算100~300之间所有能被3和7整除的数之和.
Private Sub Form_Click<>
For i = 100 To 300
If i Mod 21 = 0 Then
s = s + i
Print i;
End If
Next i
Print
Print "s="; s
End Sub
8、编程求200--400范围内5的倍数或7的倍数之和.<;一个数如果同时是7和5的倍数,则只能加一Private Sub Command1_Click<>
Dim s1%,s2%,s3%,i%
S1=0
S2=0
For i=200 to 400
If i mod 5 =0 or i mod 7=0 then
s1=s1+i
End if
Next i
For i=200 to 400
If i mod 5 =0 or i mod 7=0 then
s2=s2+i
End if
Next i
S3=s1-s2
Print s3
End sub
9、出被3、5、7除,余数为1的最小的5个正整数. Private Sub Command1_Click<>
Dim CountN%, n%
CountN = 0
n = 1
Do
n = n + 1
If n Mod 3 = 1 And n Mod 5 = 1 And n Mod 7 = 1 Then      Print n
CountN = CountN + 1
End If
Loop Until CountN = 5
End Sub
10、某次歌曲大奖赛,有7个评委.如果分别输入7个评委对某个参赛者的打分数,
按照去掉一个最高分和一个最低分的计算办法,求出该参赛者的平均得分.
Private Sub Command1_Click<>
Dim mark!, max!, min!, aver!, i%
aver = 0
For i = 1 To 7
mark = InputBox<"输入第" & i & "位评委的打分">
If i = 1 Then
max = mark: min = mark
Else
If mark < min Then min = mark
If mark > max Then max = mark
End If
aver = aver + mark
Next i
aver = <aver - min - max> / 5
Print aver
End Sub
11、编程显示100~500之间所有的水仙花数之和.<;水仙花数是3位数,其各位数之和等于该数本身Private Sub Form_Click<>
Dim i%, s%, s1%, s2%, s3%, a!
a = 0
For i = 0 To 400
s = 99 + i
s1 = <s Mod 100> \ 10
s2 = s Mod 10
s3 = s \ 100
If s1 ^ 3 + s2 ^ 3 + s3 ^ 3 = s Then
a = s + a

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。