1、vb课后习题答案华中科技大学独家A5 Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click TextBox1.Text = Val(TextBox1.Text) + 1End SubB1 Module Module1Sub Main() Dim a As Boolean Dim b As Boolean a = True b = False Console.WriteLine(0 1 2 3, a, True, b, False) Console.WriteLi
2、ne(0 1 2 3 4, a, AND, b, 为a, a And b) Console.WriteLine(0 1 2 3 4 5, a, AND, Not, b, 为a, a And Not b) Console.WriteLine(0 1 2 3 4 5 6 , a, , b, OR, b, 为a, a b Or b) Console.ReadLine()End SubEnd ModuleB2 Module Module2 Sub main() Dim x As Integer Dim y As Integer Console.Write(Input x =) x = Console.
3、ReadLine Console.Write(Input y =) y = Console.ReadLine Console.WriteLine(0 1, 表达式1的结果为, Math.Sin(x) + Math.Cos(y) Console.WriteLine(0 1, 表达式2的结果为, (x 2 + y 2) / Math.Abs(y) (1 / 3) Console.WriteLine(0 1, 表达式3的结果为, x + y x 2 - 10 * x) Console.ReadLine()End SubEnd ModuleB3 Private Sub Button1_Click(By
4、Val sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Text = Int(Rnd() * 100 + -49) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Text = Int(Rnd() * 9998 + 1) * 0.01End SubB4 Randomize()
5、 Label1.Text = Int(Rnd() * 9000 + 1000) Dim a, b, c, d As Integer a = Label1.Text 1000 b = (Label1.Text Mod 1000) 100 c = (Label1.Text Mod (a * 1000 + b * 100) 10 d = (Label1.Text Mod 10) Label2.Text = c * 1000 + a * 100 + d * 10 + bB5 Dim a As Double a = TextBox1.Text Label1.Text = Format(a, #,#.00
6、00) Label2.Text = aB10 TextBox1.Text = UCase(TextBox1.Text) TextBox1.SelectionStart = Len(TextBox1.Text)B6 Dim a = TextBox1.Text Label1.Text = Microsoft.VisualBasic.Left(a, 16) Label2.Text = Mid(a, 17, 6) Label3.Text = Microsoft.VisualBasic.Right(a, 5)B7 Dim a, b As String a = TextBox1.Text b = Text
7、Box2.Text Label1.Text = Replace(a, b, )B8 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim F = Val(TextBox1.Text), C = Val(TextBox2.Text) C = (5 / 9) * (F - 32) TextBox1.Text = Math.Round(C, 2) End Sub Private Sub Button2_Click(ByVal sen
8、der As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim F = Val(TextBox1.Text), C = Val(TextBox2.Text) F = (9 / 5) * C + 32 TextBox2.Text = Math.Round(F, 2)End SubB9 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim p
9、 = 3.14, a = Val(TextBox1.Text) TextBox2.Text = Format(p * a * a, #.00) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim p = 3.14, a = Val(TextBox1.Text) TextBox3.Text = Format(2 * p * a, #.00) End Sub Private Sub TextBox1_KeyPre
10、ss(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = Chr(13) Then If IsNumeric(TextBox1.Text) = False Then MsgBox(输入非法字符,请重新输入, 0, 友情提示) TextBox1.Text = TextBox1.Focus() End If End IfEnd SubC1 Private Sub Button1_Click(ByVal sender As
11、 System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim p = 3.14, a = Val(TextBox1.Text), b = Val(TextBox2.Text) TextBox2.Text = 2 * p * a TextBox3.Text = p * a * a If IsNumeric(TextBox1.Text) = False Then MsgBox(输入值不合法) End IfEnd SubC2 Dim x, y As Single x = InputBox(输入x=) If x 20 Th
12、en y = x 2 + 3 * x + 2 MsgBox(结果为 & y) ElseIf x 10 Then y = 0.5 + Math.Abs(x) MsgBox(结果为 & y) Else y = (3 * x) 0.5 - 2 MsgBox(结果为 & y) End If或者 Dim x, y As Single x = Val(TextBox1.Text) Select Case x Case Is 20 y = x 2 + 3 * x + 2 MsgBox(结果为 & y) Case Else y = (3 * x) 0.5 - 2 MsgBox(结果为 & y) End Sel
13、ectC3 Dim a, b, c As Single a = InputBox(输入a:) b = InputBox(输入b:) c = InputBox(输入c:) If a + b c And b + c a And a + c b Then If a = b And b = c Then MsgBox(等边) If a 2 + b 2 = c 2 Or a 2 + c 2 = b 2 Or b 2 + c 2 = a 2 Then MsgBox(直角) If (a = b Xor b = c) Or (a = c Xor a = b) Then MsgBox(等腰) End IfEnd
14、 SubC4 Dim a, b, c As Integer Randomize() Label1.Text = Int(Rnd() * (900) + 100) a = Label1.Text 100 Label2.Text = a b = (Label1.Text Mod 100) 10 Label3.Text = b c = Label1.Text Mod 10 Label4.Text = cEnd SubC5 Dim a, b, c As Integer Randomize() a = Int(Rnd() * 90 + 10) b = Int(Rnd() * 90 + 10) c = I
15、nt(Rnd() * 4 + 0) Select Case c Case 0 Label1.Text = a + b Case 1 Label1.Text = a * b Case 2 Label1.Text = a / b Case 3 Label1.Text = a - bEnd SelectC6 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a, b As Single a = TextBox1.Text If
16、a 5000 Then Label5.Text = 八折 : Label6.Text = a * 0.8 ElseIf a 3000 Then Label5.Text = 八五折 : Label6.Text = a * 0.85 ElseIf a 2000 Then Label5.Text = 九折 : Label6.Text = a * 0.9 ElseIf a 1000 Then Label5.Text = 九五折 : Label6.Text = a * 0.95 Else Label5.Text = 原价 : Label6.Text = a End If End Sub Private
17、Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Text = Label5.Text = Label6.Text = End SubC7 Dim x, y, z, t x = Val(InputBox(输入x) y = Val(InputBox(输入y) z = Val(InputBox(输入z)Label4.Text = x : Label5.Text = y : Label6.Text = zIf x x Then t =
18、 y : y = x : x = z : z = t ElseIf z y Then t = y : y = z : z = t End If End If If x y Then If z x Then t = x : x = z : z = y : y = x ElseIf z y Then t = y : y = z : z = tEnd IfEnd If Label9.Text = x : Label10.Text = y : Label11.Text = z End Sub C8 Dim a, b, c As Double a = TextBox1.Text : b = TextBo
19、x2.Text : c = TextBox3.Text If b 2 - 4 * a * c 0 Then TextBox4.Text = 无解 : TextBox5.Text = 无解 ElseIf b 2 - 4 * a * c = 0 Then TextBox4.Text = (-b + Math.Sqrt(b 2 - 4 * a * c) / 2 TextBox5.Text = (-b + Math.Sqrt(b 2 - 4 * a * c) / 2 Else TextBox4.Text = (-b + Math.Sqrt(b 2 - 4 * a * c) / 2 TextBox5.T
20、ext = (-b - Math.Sqrt(b 2 - 4 * a * c) / 2 End IfD1 Dim a, b, x Randomize() For i = 1 To 100 x = 0.01 * Int(Rnd() * 99 + 1) If x = 0.5 Then b = b + 1 End If Next TextBox1.Text = 正面 & a & % & 反面 & b & % End SubD2 Dim a, b, i For i = 2 To 100For a = 2 To i - 1 If i Mod a = 0 Then Exit For End IfNext I
21、f a i - 1 Then b = b & String.Format(Str(i) End IfNextLabel1.Text = bD3 If TextBox1.Text = 111 And TextBox2.Text = 111 Then MsgBox(登录成功!, 0, 提示信息) Else MsgBox(登录失败!, 0, 提示信息) TextBox1.Text = TextBox2.Text = TextBox1.Focus() n = n + 1 If n = 3 Then MsgBox(3次登录失败!, 0, 提示信息) : End End IfD4 Dim m%, n%,
22、x%, y%, f% m = Val(TextBox1.Text) n = Val(TextBox2.Text) If n Mod 2 0 Then MsgBox(脚数必须为偶数,请重新输入) End If For i = 0 To m f = m - i If n = 2 * i + 4 * f Then x = i y = f Exit For End If Next TextBox3.Text = x TextBox4.Text = y If x 0 Or y 0 Then MsgBox(数据错误) End If D5 Dim n As Integer, m As Single n =
23、Val(TextBox1.Text) m = 2 For i = 1 To n m = m * (2 * i) 2 / (2 * i - 1) * (2 * i + 1) Next TextBox2.Text = m End SubD6 秋水仙花数Dim a, b, c, d, n For n = 100 To 999 a = n 100 b = (n Mod 100) 10 c = n Mod 10 If n = a 3 + b 3 + c 3 Then d = d & String.Format(Str(n) Label1.Text = d End If NextD7 Dim i As I
24、nteger = 1, a, sum As Double a = 1 sum = 1 Do a = 1 / (1 / a) + i) sum = sum + a i = i + 1 Loop Until a 10 (-4) MsgBox(sum)D8 金字塔Dim i, j As Integer, s, a As String s = For i = 1 To 9 s = s + Space(9 - i) a = i For j = 1 To 2 * i - 1 s = s + a Next j s = s + vbCrLf Next i Label1.Text = s End SubD9 迭
25、代法求立方根Dim x1#, x0#, t#, a#, i# For i = 3 To 27 x0 = i a = x0 Do x1 = (2 / 3) * x0 + (a / (3 * x0 * x0) t = x0 x0 = x1 Loop Until Math.Abs(x0 - t) 10 (-5)If i = 3 Then TextBox1.Text = x1 If i = 27 Then TextBox3.Text = x1Next TextBox2.Text = 3 (1 / 3) TextBox4.Text = 27 (1 / 3) D10 Dim i, j As Integer
26、, m, n, a As Single For i = 1 To 30 m = m + 10000 Next a = 0.01 For j = 1 To 30 n = n + a a = a * 2 Next Label1.Text = Format(m, #,#.00) Label2.Text = Format(n, #,#.00) 数组E1 Dim a, b, n a = Val(TextBox1.Text) b = Val(TextBox2.Text) n = Val(TextBox3.Text) If a Mod 1 0 Then TextBox1.Focus() MsgBox(输入整
27、数!) End If If b Mod 1 0 Then TextBox2.Focus() MsgBox(输入整数!) End If If n Mod 4 0 Then TextBox3.Focus() MsgBox(输入n为4的倍数!) End If 以上为数据准备 Dim c(0 To n - 1), i, s For i = 0 To n - 1 Randomize() c(i) = Int(Rnd() * (b - a + 1) + a) s = s + Str(c(i) + If (i + 1) Mod 4 = 0 Then s = s + vbCrLf End If Next Ms
28、gBox(s) 以上为准备随机数 Dim max = c(0), min = c(0) For i = 0 To n - 1 If c(i) max Then max = c(i) If c(i) min Then min = c(i) Next 以上为求最大值最小值 MsgBox(最大值为 & max & 最小值为 & min) Dim ave, sum For i = 0 To n - 1 sum = sum + c(i) Next ave = sum / n 以上为求平均值 MsgBox(平均值为 & ave) E2 Module Module1 Sub Main() Dim arrA(99), arrB(127), a, x, z, i As Integer, y, b As String b = 0 Randomize() For i = 0 To 99 arrA(i) = Int(Rnd() * 128) Next For j = 0 To 99 For m = 0 To 127 If arrA(j) = m Then arrB(m) = arrB(m) + 1 End If Next Next For a = 48 To 57 b = Chr(a) I