vb第四章选择结构和循环结构编程题 下载本文

For z = 1 To 50 w = 40 - x - y - z

If 10 * x + 5 * y + 2 * z + w = 100 And x * y * z * w > 0 Then h = Format(x, \& Format(z, \List1.AddItem h End If Next z Next y Next x End Sub

10.找出三位数的回文数并打印,并求其个数

Private Sub Command1_Click() Dim n As Integer

For n = 101 To 999 a = Int(n / 100)

b = Int((n - a * 100) / 10) c = n - a * 100 - b * 10

If a = c Then List1.AddItem n Next

Print List1.ListCount End Sub

10-2回文字判断

1)按定义判断

先求出字符串的反序字符串,然后和原字符串比较,如果相等则是回文,否则不是回文。

Dim str As String, i As Integer

'以下求Text1中文本的反序串str For i=1 To Len(Text1)

str=Mid(Text1, i, 1) & str Next i

If str=Text1 Then MsgBox \是回文\不是回文\(2)首尾字符的成对比较 将字符串折半比较,若每对字符都相等则是回文,否则只要有一对字符不等就不是回文。

Dim i As Integer

For i=1 To Len(Text1) \\ 2

If Mid(Text1, i, 1) <> Mid(Text1, Len(Text1) - i + 1, 1) Then Exit For Next i

If i > Len(Text1) \\ 2 Then MsgBox \是回文\不是回文\

11.求1-10之间阶乘题

Private Sub Command1_Click() Dim i As Integer, s As Double i = 1

Do While i <= 10 s = 1

For j = 1 To i s = s * j Next j

Print i; \ i = i + 1 Loop End Sub

12.水仙花题

Private Sub Command1_Click() Dim i As Integer, m As Integer For i = 100 To 999 m = (i Mod 10) ^ 3 + _

(i \\ 10 Mod 10) ^ 3 + (i \\ 100) ^ 3 If m = i Then Print i Next i End Sub

Private Sub Command2_Click()

For a = 1 To 9: For b = 0 To 9: For c = 0 To 9 x = a * 100 + b * 10 + c

If x = a ^ 3 + b ^ 3 + c ^ 3 Then Print x End If Next Next Next End Sub

13.打印菱形

Private Sub Command1_Click() For i = 1 To 5

Print Spc(6 - i); For j = 1 To i

Print Spc(1); \ Next j

Print Next i

For i = 5 To 1 Step -1 Print Spc(6 - i); For j = 1 To i

Print Spc(1); \ Next j Print Next i End Sub

14.用Do循环设计例4.11中的s=1×2×3×…×n的值,其中n由

输入对话框获得。

Private Sub Command1_Click()

n = InputBox(\请输入n的值:\输入\ s = 1 i = 1 Do

If i > Val(n) Then Exit Do s = s * i i = i + 1 Loop

MsgBox \计算结果\End Sub

15. 求由数字0、1、2、3、4组成的所有无重复数字的3位正整数。

Private Sub Command1_Click()

Dim a As Integer, b As Integer, c As Integer, n As Integer For a=1 To 4

For b=0 To 4 For c=0 To 4

If a <> b And b <> c And a <> c Then

Print a * 100 + b * 10 + c;或为 List1.AddItem a * 100 + b * 10 + c

n=n + 1

If n Mod 5=0 Then Print End If Next c Next b Next a End Sub

课后题 1.

Private Sub Command1_Click()

x = Val(InputBox(\请输入一个数\显示\If x > 0 Then x = MsgBox(\是正数\显示) If x = 0 Then x = MsgBox(\是零\显示) If x < 0 Then x = MsgBox(\是负数\显示) End Sub

2.

Private Sub Command1_Click() Dim a As Single Dim b As Single Dim c As Single Dim t As Single

a = Val(InputBox(\请输入第一个数\从大到小排序\b = Val(InputBox(\请输入第二个数\从大到小排序\c = Val(InputBox(\请输入第三个数\从大到小排序\If a < b Then t = a: a = b: b = t If a < c Then t = a: a = c: c = t If b < c Then t = b: b = c: c = t

Print \从大到小的顺序为:\End Sub

3.

Private Sub Command1_Click() Static n As Integer x = Text1.Text If x = \

Label1.Caption = \欢迎进入本系统\Else

Label1.Caption = \输入错误\ n = n + 1

If n = 3 Then End End If End Sub

Private Sub Command2_Click() Text1.Text = \End Sub