1、VB试验重点章节3-1 Dim f As Single, c As SingleDim answer As IntegerPrivate Sub Cmdend_Click()Unload MeFrmmain.ShowEnd SubPrivate Sub Cmdhs_Click() f = Val(Txthua.Text) c = (f - 32) * 5 / 9 Txtshe.Text = Str$(c)End SubPrivate Sub Cmdsh_Click() c = Val(Txtshe.Text) If c = 0 Then Txthua.Text = 32 ElseIf c 0
2、Then f = (c * 9# / 5) + 32 Txthua.Text = Str$(f) Else answer = MsgBox(非法数据!, vbOKOnly + vbExclamation, 提示信息) If answer = vbOK Then Txtshe.Text = Txthua.Text = Txtshe.SetFocus End If End IfEnd SubPrivate Sub Label1_Click()End Sub3-2 Option ExplicitDim fs As IntegerPrivate Sub cmdGreat_Click() Text1.T
3、ext = UCase(Text1.Text)End SubPrivate Sub cmdRecall_Click() Text2.Text = Len(Text1.Text)End SubPrivate Sub cmdSmall_Click() Text1.Text = LCase(Text1.Text)End SubPrivate Sub Form_Load()End Sub3-3 Private Sub Form_Click() Dim s1 As Single, s2 As Single, s3 As Single Dim str1 As String, str2 As String
4、Dim b1 As Boolean, b2 As Boolean, b3 As Boolean Dim n1 As Integer, n2 As Integer, n3 As Integer s1 = 18 4 * 4# 2 / 1.6 s2 = 25 3 Mod 3.2 * Int(2.5) str1 = UCase(Xyz) & 1234 & LCase(abC) b1 = 3 4 Or 5 4 b2 = True And False n1 = Int(-3.5) + Fix(3.5) + CInt(2.6) n2 = Len(Str(123) + Hello) s3 = Sgn(7 Mo
5、d 3 - 4) + 0.75 b3 = aeoplane = 1600 Then zgz = gz * (1 + 0.1) ElseIf gz = 1400 Then zgz = gz * (1 + 0.15) Else zgz = gz * (1 + 0.2) End If txtZGz = CStr(zgz)End SubPrivate Sub Command2_Click() txtGz = txtZGz = txtGz.SetFocusEnd Sub4-4 Option ExplicitPrivate Sub Command1_Click() Dim t As Integer, y
6、As Single t = Val(txtTime) If t 10 Then y = 30 ElseIf t 50 Then y = 30 + (t - 10) * 3 Else y = 30 + (t - 10) * 2.5 End If If y = 90 Then Text2.Text = A ElseIf cj = 80 Then Text2.Text = B ElseIf cj = 70 Then Text2.Text = C ElseIf cj = 60 Then Text2.Text = D ElseIf cj = 0 Then Text2.Text = E End IfEnd
7、 SubPrivate Sub Command2_Click()Text1.Text = Text2.Text = End SubPrivate Sub Command3_Click()EndEnd SubPrivate Sub Text1_LostFocus()If Not IsNumeric(Text1.Text) Or Val(Text1.Text) 0 ThenMsgBox 数据输入错误,重输Text1.Text = Text1.SetFocusEnd IfEnd Sub4-6 Option ExplicitPrivate Sub Form_Load() Dim Y As Intege
8、r, m As Integer Dim d As Integer, w1 As Integer Dim Rq As Date Dim w2 As String Rq = Date Y = Year(Rq) m = Month(Rq) d = Day(Rq) w1 = Weekday(Rq) Select Case w1 Case 1 w2 = 日 Case 2 w2 = 一 Case 3 w2 = 二 Case 4 w2 = 三 Case 5 w2 = 四 Case 6 w2 = 五 Case 7 w2 = 六 End Select Label1.Caption = 今天是 & Y & 年 &
9、 m & 月 & d & 日 & 星期 & w2End Sub4-7 Private Sub Command1_Click()Dim Y1 As Integer, M1 As IntegerDim flag As Boolean, D1 As Integer, S1 As StringY1 = Val(txtYear)M1 = Val(txtMonth)flag = (Y1 Mod 4 = 0 And Y1 Mod 100 0) Or Y1 Mod 400 = 0Select Case M1Case 1, 2, 3 D1 = 31: S1 = 一季度 If M1 = 2 Then If fla
10、g Then D1 = 29 Else D1 = 28 End If End IfCase 4, 5, 6 D1 = 30: S1 = 二季度If M1 = 5 Then D1 = 31Case 7, 8, 9 D1 = 31: S1 = 三季度If M1 = 9 Then D1 = 30Case 10, 11, 12 D1 = 31: S1 = 四季度If M1 = 11 Then D1 = 30End SelectIf flag Then txtRun = 是闰年 Else txtRun = 不是闰年 End IftxtSea = S1txtDays = Str(D1)End SubPri
11、vate Sub Command2_Click()txtYear = txtMonth = txtSea = txtRun = txtDays = End Sub4-8 Private Sub Cmdclear_Click() txta.Text = Txtb.Text = Txtc.Text = Txtx1.Text = Txtx2.Text = txta.SetFocusEnd SubPrivate Sub Cmdcup_Click() a = Val(txta.Text) b = Val(Txtb.Text) c = Val(Txtc.Text) disc = b * b - 4 * a
12、 * c p = -b / (2 * a) q = Sqr(Abs(disc) / (2 * a) If disc = 0 Then Txtx1 = Format$(p + q, #0.#) Txtx2 = Format$(p - q, #0.#) Else Txtx1 = Str$(p) & + & Str$(q) & i Txtx2 = Str$(p) & - & Str$(q) & i End IfEnd SubPrivate Sub Cmdend_Click() EndEnd SubPrivate Sub txta_KeyPress(KeyAscii As Integer)If Key
13、Ascii = 13 Then If Not IsNumeric(txta.Text) Then MsgBox 数据非法,重输 txta.Text = txta.SetFocus Else Txtb.SetFocus End If End If End SubPrivate Sub Txtb_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then If Not IsNumeric(Txtb.Text) Then MsgBox 数据非法,重输 Txtb.Text = Txtb.SetFocus Else Txtc.SetFocus End If En
14、d IfEnd SubPrivate Sub Txtc_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then If Not IsNumeric(Txtc.Text) Then MsgBox 数据非法,重输 Txtc.Text = Txtc.SetFocus Else Cmdcup.SetFocus End If End IfEnd Sub4-9 Private Sub Cmdclear_Click()txtm1 = txtm2 = txtm3 = Lblinfo.Caption = txtm1.SetFocusEnd SubPrivate Sub
15、 Cmdsort_Click() Dim m1 As Integer, m2 As Integer, m3 As Integer m1 = Val(txtm1.Text) m2 = Val(txtm2.Text) m3 = Val(txtm3.Text) If (m1 + m2 + m3) 3 95 Or (m1 = 100 And m2 = 100 And m3 = 80) _ Or (m1 = 100 And m3 = 100 And m2 = 80) Or _ (m1 = 80 And m2 = 100 And m3 = 100) Then Lblinfo.Caption = 此学生奖学
16、金为一等奖 ElseIf (m1 + m2 + m3) 3 90 Or (m1 = 100 And m2 = 75 And m3 = 75) _ Or (m1 = 75 And m2 = 100 And m3 = 75) Or _ (m1 = 75 And m2 = 75 And m3 = 100) Then Lblinfo.Caption = 此学生奖学金为二等奖 ElseIf m1 = 70 And m2 = 70 And m3 = 70 Then Lblinfo.Caption = 此学生奖学金为三等奖 Else Lblinfo.Caption = 此学生无奖学金 End IfEnd S
17、ubPrivate Sub txtm1_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then If Not IsNumeric(txtm1) Then MsgBox 数据非法,重新输入, vbExclamation, 输入操作数1 txtm1.Text = txtm1.SetFocus Else txtm2.SetFocus End IfEnd IfEnd SubPrivate Sub txtm2_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then If Not IsNumeric(txtm2)
18、Then MsgBox 数据非法,重新输入, vbExclamation, 输入操作数2 txtm2.Text = txtm2.SetFocus Else txtm3.SetFocus End IfEnd IfEnd SubPrivate Sub txtm3_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then If Not IsNumeric(txtm3) Then MsgBox 数据非法,重新输入, vbExclamation, 输入操作数3 txtm3.Text = txtm3.SetFocus Else Cmdsort.SetFocus
19、End IfEnd IfEnd Sub5-1 Option Explicit方法一:用三重循环,将三个一位数合并成一个三位数Private Sub Form_Click()Dim i As Integer, j As Integer, k As IntegerFor i = 1 To 9 For j = 0 To 9 For k = 0 To 9 If i * 100 + j * 10 + k = i 3 + j 3 + k 3 Then Print Str$(i * 100 + j * 10 + k); End If Next k Next j Next iEnd Sub 方法二:用一重循环
20、,将一个三位数分离为三个一位数Private Sub Form_Click() Dim n As Integer Dim i As Integer, j As Integer, k As Integer For n = 100 To 999 i = n Mod 10 j = (n 10) Mod 10 k = n 100 If n = i 3 + j 3 + k 3 Then Print Str$(n); Next nEnd Sub5-2 Private Sub Command1_Click() Dim n As Integer, i As Integer, s As String, sum
21、As Integer For n = 1 To 1000 sum = 0 s = Str(n) + = For i = 1 To n 2 If n Mod i = 0 Then sum = sum + i s = s + Str(i) + + End If Next i If n = sum Then s = Left(s, Len(s) - 1) Picture1.Print s End If Next nEnd SubPrivate Sub Command2_Click() EndEnd Sub5-3 Private Sub Form_Click()Dim n As Integer, x0 As Single, x1 As Single, sum!, x!x = Val(Text1.Text)n = 1sum = 1x1 = 1Do x0 = x1 x1 = x0 * x / n sum = sum + x1 n = n + 1Loop Until x1 0.000001 Print sumEnd Sub5-4 Private Sub cmdClear_Click() txt1.Text = txt2.Text = txt1.SetFocusEnd Sub
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1