大学vb程序设计课后习题三参考答案2014

第三章教材习题解答

一、判断题

1.√

2.×

3.√

4.×

5.√

6.×

7.×

8.√

9.× 10.√

二、选择题

1.A 2.A 3.D 4.C 5.C 6.D 7.A 8.B

三、填空题

1.If x>y Then t=x: x=y: y=t 2.Is

3.For i=0 To 9: b(i) = InputBox("b(" & i & ")="): Next i

4.ReDim a(n) As Single 5.String 6.8

四、程序阅读题

大学vb程序设计课后习题三参考答案2014

五、程序填空题

大学vb程序设计课后习题三参考答案2014

六、程序设计题

题1.程序代码如下

Private Sub Form_Click()

Dim a As Single, b As Single, c As Single

Dim x As Single, y As Single, z As Single

a = Val(InputBox("a=")):

b = Val(InputBox("b=")):

c = Val(InputBox("c="))

If a > b Then x = a Else x = b

If x < c Then x = c

If a < b Then z = a Else z = b

If z > c Then z = c

y = a + b + c - x - z

Print x; y; z

End Sub

题2.程序代码如下

用IF语句

Private Sub Command1_Click() Dim y As Single, x As Single x = Val(InputBox("x="))

If x > 3 Then

y = x + 3

ElseIf x >= 1 Then

y = x * x

ElseIf x > 0 Then

y = Sqr(x)

Else

y = 0

End If

Print y

End Sub 用Select语句

Private Sub Command2_Click() Dim y As Single, x As Single x = Val(InputBox("x="))

Select Case x

Case Is > 3: y = x + 3

Case Is >= 1: y = x * x

Case Is > 0: y = Sqr(x)

Case Else: y = 0

End Select

Print y

End Sub

题3.程序代码如下

Private Sub Form_Click()

Dim i As Byte, j As Byte

For i = 1 To 9

For j = 1 To i

Print Trim(Str(i)); "*"; Trim(Str(j)); "="; Trim(Str(i * j));

If i * j < 10 Then Print Space(3); Else Print Space(2);

Next j

Print

Next i

End Sub

题4.程序代码如下

Private Sub Command1_Click()

Dim x As Double, s As Double, t As Double, i As Integer, n As Integer x = Val(InputBox("x=")): n = Val(InputBox("n="))

t = 1: s = 1 + x

For i = 2 To n + 1

t = t * x / i: s = s + t

Next i

Print s

End Sub

题5.程序代码如下

Private Sub Command1_Click()

Dim e As Single, t As Single

Dim i As Integer

e = 2: t = 1: i = 2

Do While t >= 0.0001

t = t / i: i = i + 1: e = e + t

Loop

Print e

End Sub

题6.程序代码如下

Private Sub Command1_Click()

Dim i As Long, k As Integer

For i = 1 To 1000

k = Len(Trim(Str(i)))

If (i * i - i) Mod 10 ^ k = 0 Then Print i; i * i

Next i

End Sub

题7.程序代码如下

Private Sub Command1_Click()

Dim x(10) As Single, y(10) As Single, s As Single

Dim i As Integer, j As Integer

For i = 1 To 10

x(i) = Val(InputBox("x(" & i & ")="))

y(i) = Val(InputBox("y(" & i & ")="))

Next i

For i = 1 To 9

For j = i + 1 To 10

s = s + Sqr((x(i) - x(j)) ^ 2 + (y(i) - y(j)) ^ 2)

Next j

Next i

Print s

End Sub

题8.程序代码如下

Private Sub Form_Click()

Dim m As Integer, n As Integer, i As Integer, j As Integer m = Val(InputBox("请输入a数组元素个数:"))

n = Val(InputBox("请输入b数组元素个数:"))

ReDim a(m) As Integer, b(n) As Integer

For i = 1 To m: a(i) = Val(InputBox("a(" & i & ")=")): Next i

For i = 1 To n: b(i) = Val(InputBox("b(" & i & ")=")): Next i

For i = 1 To m

For j = 1 To n: If a(i) = b(j) Then Exit For: Next j

If j > n Then Print a(i)

Next i

For i = 1 To n

For j = 1 To m: If b(i) = a(j) Then Exit For: Next j

If j > m Then Print b(i)

Next i

End Sub

题9.程序代码如下

Private Sub Form_Click()

Dim x(5, 5) As Integer, i As Integer, j As Integer

For i = 1 To 5

For j = 1 To 6 - i: x(i, j) = 7 - i - j: Next j

Next i

For i = 2 To 5

For j = 7 - i To 5: x(i, j) = i + j - 5: Next j

Next i

For i = 1 To 5

For j = 1 To 5: Print x(i, j); : Next j

Print

Next i

End Sub

题10.程序代码如下

(1) 显示呈直角三角形

Private Sub Command1_Click()

Dim i As Byte, j As Byte, n As Byte

Form1.Cls

Do

n = Val(InputBox("n="))

Loop Until n > 0 And n < 11

ReDim a(n + 1, n + 1)

For i = 1 To n + 1: a(i, 1) = 1: a(i, i) = 1: Next i

For i = 3 To n + 1

For j = 2 To n: a(i, j) = a(i - 1, j - 1) + a(i - 1, j): Next j

Next i

For i = 1 To n + 1

For j = 1 To i: Print Tab((j - 1) * 4); Trim(Str(a(i, j))); : Next j

Print

Next i

End Sub

(2) 显示呈等腰三角形

Private Sub Command2_Click()

Dim i As Integer, j As Integer, n As Integer

Form1.Cls

Do

n = Val(InputBox("n="))

Loop Until n > 0 And n < 11

ReDim a(n + 1, n + 1)

For i = 1 To n + 1: a(i, 1) = 1: a(i, i) = 1: Next i

For i = 3 To n + 1

For j = 2 To n: a(i, j) = a(i - 1, j - 1) + a(i - 1, j): Next j Next i

For i = 1 To n + 1

Print Space(2 * (n + 1 - i) + 1);

For j = 1 To i

Print Space(4 - Len(Trim(Str(a(i, j))))); Trim(a(i, j));

Next j

Print

Next i

End Sub

推荐阅读

相关文档