VB实验参考答案.doc
VB.NET程序设计实验参考答案实验1-3Dim n% = 0 Private Sub Form1_Click() Handles Me.Click n = n + 1 TextBox1.Text = nEnd Sub实验1-4Private Sub Form1_Load() Handles MyBase.Load Me.Text = "装入窗体" Me.BackgroundImage = Image.FromFile("tongji.bmp") '当前目录为Bin的Debug Me.Cursor = New Cursor("key04.ico") Me.Icon = New Icon("MISC34.ICO") End Sub Private Sub Form1_Click() Handles MyBase.Click Me.Text = "单击窗体" ' Me.Size = New Size(264 + 10, 256 + 30) '图片大小为× Me.BackgroundImage = Image.FromFile("tongji-2.bmp") End Sub Private Sub Form1_DoubleClick() Handles MyBase.DoubleClick Me.Text = "双击窗体" Me.MaximizeBox = False Me.MinimizeBox = False Me.BackgroundImage = Nothing Me.Cursor = Cursors.Default End Sub Private Sub Form1_Resize() Handles Me.Resize Me.Size = New Size(264 + 10, 256 + 30) Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3DEnd Sub实验1-5Private Sub Button1_Click() Handles Button1.Click TextBox1.Font = New Font("隶书", 20) End Sub Private Sub Button2_Click() Handles Button2.Click TextBox2.Text = TextBox1.SelectedText TextBox2.Font = TextBox1.FontEnd Sub实验1-6Dim w%, h% Private Sub Form1_Load() Handles Me.Load w = PictureBox1.Width h = PictureBox1.Height End Sub Private Sub Button2_Click() Handles Button2.Click PictureBox1.Width = w PictureBox1.Height = h End Sub Private Sub Button1_Click() Handles Button1.Click, Button1.Click PictureBox1.Width = PictureBox1.Width / 2 PictureBox1.Height = PictureBox1.Height / 2End Sub实验2-1Private Sub Button1_Click() Handles Button1.Click TextBox1.Text = Int(Rnd() * 10 + 0) TextBox2.Text = Int(Rnd() * 90 + 10) TextBox3.Text = Int(Rnd() * (999 - 100 + 1) + 100) End Sub Private Sub Button2_Click() Handles Button2.Click Dim avg! avg = (Val(TextBox1.Text) + Val(TextBox2.Text) + Val(TextBox3.Text) / 3 Label2.Text = "三个数的平均值为:" & Format(avg, "0.00") End Sub Private Sub Button3_Click() Handles Button3.Click EndEnd Sub实验2-4Private Sub Button1_Click() Handles Button1.Click Dim n% n = Val(InputBox("输入个数") Label2.Text = Button1.Text & "函数的结果" TextBox2.Text = StrDup(n, TextBox1.Text) End Sub Private Sub Button2_Click() Handles Button2.Click Label2.Text = Button2.Text & "函数的结果" TextBox2.Text = UCase(TextBox1.Text) End Sub Private Sub Button3_Click() Handles Button3.Click Label2.Text = Button3.Text & "函数的结果" TextBox2.Text = LCase(TextBox1.Text) End Sub Private Sub Button4_Click() Handles Button4.Click Dim n, m As Integer n = Val(InputBox("输入n") m = Val(InputBox("输入m") TextBox2.Text = Mid(TextBox1.Text, n, m) Label2.Text = Button4.Text & "函数的结果" End Sub Private Sub Button5_Click() Handles Button5.Click Label2.Text = Button5.Text & "函数的结果" TextBox2.Text = Len(TextBox1.Text) End Sub Private Sub Button6_Click() Handles Button6.Click Dim s1$ s1 = Val(InputBox("输入s1") TextBox2.Text = InStr(TextBox1.Text, s1) Label2.Text = Button6.Text & "函数的结果" End Sub Private Sub Button7_Click() Handles Button7.Click Dim s1$, news$ s1 = InputBox("输入s1") news = InputBox("输入news") TextBox2.Text = Replace(TextBox1.Text, s1, news) Label2.Text = Button7.Text & "函数的结果" End Sub Private Sub Button8_Click() Handles Button8.Click Label2.Text = Button8.Text & "函数的结果" TextBox2.Text = Trim(TextBox1.Text) End Sub实验2-5Private Sub Button1_Click() Handles Button1.Click Label2.Text = Button1.Text & "函数的结果" TextBox2.Text = Val(TextBox1.Text) End Sub Private Sub Button2_Click() Handles Button2.Click Label2.Text = Button2.Text & "函数的结果" TextBox2.Text = Str(TextBox1.Text) End Sub Private Sub Button3_Click() Handles Button3.Click Label2.Text = Button3.Text & "函数的结果" TextBox2.Text = Chr(TextBox1.Text) End Sub Private Sub Button4_Click() Handles Button4.Click TextBox2.Text = Asc(TextBox1.Text) Label2.Text = Button4.Text & "函数的结果" End Sub实验3-2 Private Sub Button1_Click() Handles Button1.Click Dim x!, y!If Not IsNumeric(TextBox1.Text) Then MsgBox("error") TextBox1.Text = "" TextBox1.Focus() Else x = TextBox1.Text If x < 10 Then y = 30 ElseIf x < 50 Then y = x * 2.5 Else y = x * 2 End If If y > 150 Then y = 150 End If TextBox2.Text = y End Sub 实验3-7Private Sub Button1_Click() Handles Button1.Click Dim a#, b#, c#, dt!, s1$, s2$ a = Val(TextBox1.Text) b = Val(TextBox2.Text) c = Val(TextBox3.Text) dt = b 2 - 4 * a * c If dt > 0 Then ' 实根 dt = Math.Sqrt(dt) Label3.Text = Format(-b + dt) / 2 / a, "0.00") Label5.Text = Format(-b - dt) / 2 / a, "0.00") ElseIf dt = 0 Then ' 重根 dt = Math.Sqrt(dt) Label3.Text = Format(-b / 2 / a, "0.00") Label5.Text = Format(-b / 2 / a, "0.00") Else dt = Math.Sqrt(-dt) ' 复根 s1 = Format(-b / 2 / a, "0.00") s2 = Format(dt / 2 / a, "0.00") Label3.Text = s1 & "+" & s2 & "i" Label5.Text = s1 & "-" & s2 & "i" End If End Sub实验3-8'If 语句 Dim n%, s$ n = InputBox("输入一个数字7") If n = 1 Then s = "星期一" ElseIf n = 2 Then s = "星期二" ElseIf n = 3 Then s = "星期三" ElseIf n = 4 Then s = "星期四" ElseIf n = 5 Then s = "星期五" ElseIf n = 6 Then s = "星期六" Else s = "星期日" End If MsgBox(s) 'Select语句: Select Case n Case 1 s = "星期一" Case 2 s = "星期二" Case 3 s = "星期三" Case 4 s = "星期四" Case 5 s = "星期五" Case 6 s = "星期六" Case 7 s = "星期日" End Select ' Choose(函数) s = Choose(n, "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日")实验4-2Private Sub Button1_Click() Handles Button1.Click Dim i%, c$ Label1.Text = "" For i = 1 To 9 c = StrDup(18 - 2 * i, Chr(i + 64) label1.text &= Space(i) & c & vbCrLf Next iEnd Sub实验4-5Private Sub Button1_Click() Handles Button1.Click 三重循环 Dim i%, j%, k%, t% Label1.text = "" For i = 1 To 9 For j = 0 To 9 For k = 0 To 9 t = i * 100 + j * 10 + k If t = i 3 + j 3 + k 3 Then Label1.text &= t & vbCrLf End If Next k Next j Next iEnd Sub或者:Private Sub Button2_Click() Handles Button2.Click 单重循环 Dim i%, j%, k%, t% Label2.Text = "" For t = 100 To 999 i = t 100 j = (t Mod 100) 10 k = t Mod 10 If t = i 3 + j 3 + k 3 Then Label2.Text &= t & vbCrLf End If Next tEnd Sub实验4-7Private Sub Form1_Load() Handles Me.Load HScrollBar1.Maximum = 1 HScrollBar1.Maximum = 9 HScrollBar2.Minimum = 5 HScrollBar1.Maximum = 10 End Sub Private Sub HScrollBar1_Scroll1() Handles HScrollBar1.Scroll Label1.Text = "a=" & HScrollBar1.Value End SubPrivate Sub HScrollBar2_Scroll() Handles HScrollBar2.Scroll Dim a, n, i As Integer, sum&, t& Label2.Text = "n=" & HScrollBar2.Value a = HScrollBar1.Value n = HScrollBar2.Value Label3.Text = "sum=" & vbCrLf sum = 0 : t = 0 For i = 1 To n t = t * 10 + a Label3.Text &= Space(15 - Len(Trim(t) & t & vbCrLf sum = sum + t Next Label3.Text &= "-" & vbCrLf Label3.Text &= Space(15 - Len(Trim(t) & sumEnd Sub实验4-10Dim t% Private Sub TextBox1_KeyPress() Handles TextBox1.KeyPress If Asc(e.KeyChar) = 13 Then ProgressBar1.Minimum = 0 t = Val(TextBox1.Text) * 60 ProgressBar1.Maximum = t ProgressBar1.Value = t Timer1.Interval = 1000 Timer1.Enabled = True End If End Sub Private Sub Timer1_Tick() Handles Timer1.Tick t = t - 1 TextBox1.Text = t 60 & ":" & Format(t Mod 60, "00") ProgressBar1.Value = t If t = 0 Then Timer1.Enabled = FalseEnd Sub实验4-13Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click Dim x%, y%, z%, a, b, c Label1.Text = " x y z" & vbCrLf For x = 1 To 6 For y = 1 To 6 For z = 5 To 6 If x < y And y < z Then a = Choose(x, "星期一", "星期二", "星期三", "星期四", "星期五", "星期六") b = Choose(y, "星期一", "星期二", "星期三", "星期四", "星期五", "星期六") c = Choose(z, "星期一", "星期二", "星期三", "星期四", "星期五", "星期六") Label1.Text &= " " & a & " " & b & " " & c & vbCrLf End If Next Next NextEnd Sub实验5-1Private Sub Form1_Click() Handles Me.Click Dim s(9) As Integer, i, Min, Max, Aver s(0) = 30 + Int(Rnd() * 70) Min = s(0) Max = s(0) Aver = s(0) Label1.Text = s(0) For i = 1 To 9 s(i) = 30 + Int(Rnd() * 70) Label1.Text &= Space(4) & s(i) Aver += s(i) If s(i) < Min Then Min = s(i) If s(i) > Max Then Max = s(i) Next i Aver = Aver / 10 Label1.Text &= vbCrLf Label1.Text &= "Min=" & Min & " Max=" & Max & " Average=" & Aver & vbCrLfEnd Sub实验5-3Dim mark(19) As Integer Private Sub Button1_Click() Handles Button1.Click ' 产生 Dim i% For i = 0 To 19 mark(i) = Int(Rnd() * 100) Label1.Text &= mark(i) & Space(6 - Len(Str(mark(i) If (i+1) Mod 5 = 0 Then Label1.Text &= vbCrLf Next i End Sub Private Sub Button2_Click() Handles Button2.Click ' 统计 Dim s%(9), i%, k% For i = 0 To 19 k = mark(i) 10 Select Case k Case 0 To 5 ' 不及格人数 s(5) = s(5) + 1 Case 9 To 10 ' 优秀人数 s(9) = s(9) + 1 Case 6 To 8 ' 其他三个分数段的人数 s(k) = s(k) + 1 End Select Next i For i = 5 To 9 Label2.Text &= "s(" & i & ")的人数有 " & s(i) & "个" & vbCrLf NextEnd Sub实验5-4Dim a%(9), i%, j%, imax%, m Private Sub Button1_Click() Handles Button1.Click 产生 Randomize() For i = 0 To 9 a(i) = Int(Rnd() * 90) + 10 Label1.Text &= " " & a(i) Next End Sub Private Sub Button2_Click() Handles Button2.Click 排序 For i = 0 To 8 imax = i For j = i + 1 To 9 If a(j) > a(imax) Then imax = j End If Next m = a(i) a(i) = a(imax) a(imax) = m Next For i = 0 To 9 Label2.Text &= " " & a(i) Next End Sub实验5-6Dim n%, a%(100, 100), i%, j% Private Sub TextBox1_KeyPress() Handles TextBox1.KeyPress If Asc(e.KeyChar) = 13 Then n = Val(TextBox1.Text) For i = 0 To n - 1 For j = 0 To i If i = 0 Or j=0 Or i=j Then a(i, j) = 1 Else a(i, j) = a(i - 1, j - 1) + a(i - 1, j) End If Label1.Text &= a(i, j) & Space(4 - Len(Trim(a(i, j) Next Label1.Text &= vbCrLf Next End IfEnd Sub实验5-8Private Sub Form1_Load() Handles Me.Load ListBox1.Sorted = True ListBox2.Sorted = True ListBox1.Items.Clear() ListBox1.Items.Add("大学计算机基础") ListBox1.Items.Add("C/C+程序设计") ListBox1.Items.Add("VB程序设计") ListBox1.Items.Add("Web程序设计") ListBox1.Items.Add("多媒体技术与应用") ListBox1.Items.Add("数据库技术与应用") ListBox1.Items.Add("网络技术与应用") ListBox1.Items.Add("硬件技术基础") ListBox1.Items.Add("软件技术技术基础") End Sub Private Sub ListBox1_Click() Handles ListBox1.Click If ListBox2.Items.Count >= 5 Then MsgBox("超过门课程,不能再选") Exit Sub Else ListBox2.Items.Add(ListBox1.Text) ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) End IfEnd Sub实验5-9Private Sub ComboBox1_KeyPress() Handles ComboBox1.KeyPress Select Case Asc(e.KeyChar) Case 48 To 57, 13 If Asc(e.KeyChar) = 13 Then ComboBox1.Items.Add(ComboBox1.Text) ComboBox1.Text = "" End If Case Else e.Handled = True End Select End Sub Private Sub Button1_Click() Handles Button1.Click Dim min, imin, max, imax, t, i As Integer max = ComboBox1.Items(0) min = ComboBox1.Items(0) imin = 0 imax = 0 For i = 1 To ComboBox1.Items.Count - 1 If Val(ComboBox1.Items(i) > max Then max = ComboBox1.Items(i) imax = i End If If Val(ComboBox1.Items(i) < min Then min = ComboBox1.Items(i) imin = i End If Next t = ComboBox1.Items(ComboBox1.Items.Count - 1) Combo