VB編程練習(xí)答案_第1頁
VB編程練習(xí)答案_第2頁
VB編程練習(xí)答案_第3頁
VB編程練習(xí)答案_第4頁
VB編程練習(xí)答案_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、一.選擇結(jié)構(gòu)'-'題目:編寫函數(shù)fun,函數(shù)的功能是:根據(jù)一個百分制' 成績mark(整數(shù)),顯示對應(yīng)五級制的評定。條件如下:' mark大于等于90顯示"優(yōu)秀"' mark小于90且大于等于80顯示"良好"' mark小于80且大于等于70顯示"中等"' mark小于70且大于等于60顯示"及格"' mark小于60顯示"不及格"' 要求使用IF語句來實現(xiàn)。'-Private Function fun(mark A

2、s Integer) As String'*Program*If mark >= 90 Then fun = "優(yōu)秀"ElseIf mark >= 80 Then fun = "良好"ElseIf mark >= 70 Then fun = "中等"ElseIf mark >= 60 Then fun = "及格"Else fun = "不及格"End If'* End *End FunctionPrivate Sub Form_Load() Show P

3、rint fun(90)End Sub'-'題目:編寫函數(shù)fun,函數(shù)的功能是:判斷一個字符是: 字母字符、數(shù)字字符還是其他字符,并做相應(yīng)的顯示。' 字母字符顯示字符串"字母"' 數(shù)字字符顯示字符串"數(shù)字"' 其他字符顯示字符串"其他" ' 要求使用select語句來實現(xiàn)'-Private Function fun(n As String) As String'*Program*Select Case nCase "A" To "Z"

4、;, "a" To "z" fun = "字母"Case "0" To "9" fun = "數(shù)字"Case Else fun = "其他"End Select'* End *End FunctionPrivate Sub Form_Load() Show Print fun("A") Print fun("9") Print fun("&")End Sub二.一般循環(huán)'-

5、'題目:(事件)雙擊窗體。' (響應(yīng))求出100200之間,能被5整除,但不能被3整除的數(shù)。輸出這些數(shù)并求所有數(shù)之和,存入變量中SUM中。 輸出sum 應(yīng)為 2100'-Private Sub Form_dblClick()Dim sum As Integersum = 0'* Program *Dim i As IntegerFor i = 100 To 200 If (i Mod 5) = 0) And (i Mod 3) <> 0) Then Print (CStr(i) sum = sum + i End IfNext'* End *

6、Print sumEnd Sub'-'題目:(事件)單擊窗體。 輸出L 應(yīng)為 46189' (響應(yīng))求1020之間所有素數(shù)的乘積并輸出在窗體上。將結(jié)果存入變量L中。'-Private Sub Form_Click()Dim l As Long'* Program * Dim i As Integer Dim j As Integer Dim b As Boolean l = 1 b = False For i = 10 To 20 For j = 2 To i - 2 If i Mod j = 0 Then b = True End If Next If

7、 b = False Then l = l * i End If b = False Next Print Str(l)'* End *End Sub'-'題目:(事件)單擊窗體。' (響應(yīng))編寫程序求10×11×12+11×12×13+15×16×17的結(jié)果,' 并將結(jié)果輸出到窗體上。將結(jié)果存入變量M中 輸出M 應(yīng)為 15390'-Private Sub Form_Click()Dim m As Long'* Program *Dim i, j As IntegerDim s

8、As LongFor i = 10 To 15 s = 1 For j = i To i + 2 s = s * j Next m = m + sNextPrint (CStr(m)'* End *End Sub'-'題目:(事件)單擊窗體。' (響應(yīng))在窗體上打印數(shù)列2/1,3/2,5/3,8/5,13/8 的前10項,并求其和, 將和保存在變量s中 輸出:'-Private Sub Form_Click()Dim s As Single'* Program *Dim i, j, k, m As Integers = 0i = 2j = 1Fo

9、r k = 1 To 10 s = s + i / j m = j Print (CStr(i) & "/" & CStr(j) j = i i = i + mNext'* End *End Sub'-'題目:編寫函數(shù)fun其功能是:根據(jù)整型形參m,計算如下公式的值:' y=11/31/51/7.1/(2m+1) 例如:若m=9,則應(yīng)輸出:2.133255'-Private Function fun(m As Integer) As Single Dim y As Double'* Program * Dim

10、y As Double Dim i As Integer y = 1 For i = 1 To m y = y + 1 / (2 * i + 1) Next fun = y'* End *End FunctionPrivate Sub Form_Load() Show Print fun(9)End Sub'-'題目:函數(shù)fun其功能是:根據(jù)整型形參m,計算如下公式的值:y=1/21/41/6.1/2m' 例如:'-Private Function fun(m As Integer) As Double'* Program * Dim y As

11、Double Dim i As Integer For i = 1 To m y = y + 1 / (2 * i) Next fun = y'* End *End FunctionPrivate Sub Form_Load() Show Print fun(9)End Sub'-'題目: (事件)單擊窗體。' (響應(yīng))已知sum=1-1/4+1/9-1/16+1/25.-1/100, 輸出 sum 應(yīng)為 0.8179622' 請將sum結(jié)果值輸出到窗體上。 使用for.next語句完成程序'-Private Sub Form_Click() D

12、im sum As Single'* Program *Dim i As Integerfor i = 1 to 10sum = sum + (-1) (i - 1) /(i*i)nextPrint sum '* End *End Sub三.級數(shù)'-'題目: (事件)單擊窗體。' (響應(yīng))已知sum=1/2!-1/4!+1/6!-1/8!+1/10!,請將sum結(jié)果值輸出到窗體上。' 使用do while.loop語句完成程序 輸出sum 應(yīng)為 0.4596977'-Private Sub Form_Click() Dim p As Lo

13、ng 'p變量存放階乘的值 Dim sum As Single'* Program *Dim i As IntegerDim n As Integeri = 1p = 1Do While i <= 10 p = p * iIf i Mod 2 = 0 Then n = n + 1 sum = sum + (-1) (n - 1) / pEnd If i = i + 1LoopPrint sum'* End *End Sub'-'題目:(事件)單擊窗體。' (響應(yīng))已知S=1+1/4+1/9+1/16+1/25+1/100,結(jié)果輸出在窗體上。

14、輸出s 應(yīng)為 1.291286'-Private Sub Form_Click()Dim s As Single'* Program *Dim i As Integers = 0For i = 1 To 10 s = s + 1 / (i i)NextPrint (s)'* End *End Sub'-'題目: (事件)單擊窗體。' (響應(yīng))求sum=1+1+2+3+5+8+13+.+144的和,請將sum結(jié)果值輸出到窗體上。使用 do.loop語句完成程序 輸出sum 應(yīng)為 376'-Private Sub Form_Click() D

15、im sum As Long'* Program *Dim i As IntegerDim s1 As IntegerDim s2 As Integeri = 1s1 = 1: s2 = 1Sum = Sum + s1 + s2Do If s2 = 144 Then Exit Do End If s1 = s1 + s2 s2 = s1 + s2 Sum = Sum + s1 + s2 LoopPrint Sum'* End *End Sub'-'題目:編寫函數(shù)fun其功能是:根據(jù)整型形參m,計算如下公式的值:' y=1/5+1/6+1/7+1/8+1/

16、9+1/10.+1/(m+5) 例如:若m=9,則應(yīng)輸出:1.168229'要求:函數(shù)中用到的中間變量必須聲明為Single類型。'-Private Function Fun(m As Integer) As Single'* Program * Dim y As Double Dim i As Integer For i = 0 To m y = y + 1 / (i + 5) Next Fun = y'* End *End FunctionPrivate Sub Form_Load() Show Print Fun(9)End Sub四.數(shù)字關(guān)系'-

17、'題目:編寫函數(shù)fun其功能是: 判斷一個整數(shù)w的各位數(shù)字平方之和能否被5整除 , 可以被5整除則返回1 , 否則返回0'-Private Function fun(w As Integer) As Boolean'* Program * Dim k As Integer, s As Integer Do s = s + (w Mod 10) * (w Mod 10) w = Int(w / 10) Loop While w <> 0 If s Mod 5 = 0 Then k = 1 Else k = 0 End If fun = k'* End

18、*End FunctionPrivate Sub Form_Load() Show Print fun(50) '輸出True Print fun(51) '輸出FalseEnd Sub'-'題目:(事件)雙擊窗體。' (響應(yīng))寫程序解決百錢買百雞問題:公雞3元1只,母雞5元1只,小雞一元3只,怎樣用100元買100只雞。把結(jié)果輸出到窗體上。將答案數(shù)存放在變量N中。 輸出'- Private Sub Form_dblClick()Dim n As Integern = 0'* Program *Dim i, j, k As Integer

19、For i = 1 To 20 '母雞 For j = 1 To 33 '公雞 For k = 3 To 100 '小雞 If (k Mod 3) = 0) And (i * 5 + j * 3 + k / 3) = 100) And (i + j + k) = 100) Then Print (CStr(i) Print (CStr(j) Print (CStr(k) n = n + 1 End If Next NextNext'* End *End Sub'-'題目: (事件)單擊窗體。' (響應(yīng))輸出所有的三位數(shù),它滿足該數(shù)中有某兩

20、位為相同的數(shù), 且該數(shù)是一個完全平方數(shù)。' 求出所有這些數(shù)的和,并將結(jié)果存入變量SUM中。 輸出'-Private Sub Form_Click() Dim sum As Integer'* Program *dim a as integer,b as integerdim b3 as integer,b2 as integer,b1 as integerfor a=10 to 31b=a*ab3=b100b2=(b10) mod 10b1=b mod 10if b1=b2 or b1=b3 or b2=b3 then? a,bsum=sum+bend ifnext a

21、Print sum'* End *End Sub'-'題目:(事件)單擊窗體。' (響應(yīng))求出1000-9999之間具有如下特點的四位數(shù)字, 它的平方根恰好就是它中間的兩位數(shù)字,例如,2500開平方為50,恰為2500的中間兩位, 輸出 找出所有這樣的四位數(shù)。并求出所有這樣的數(shù)的和存入SUM中'-Private Sub Form_Click()Dim sum As Integersum = 0'* Program *Dim i As Integer Dim j As Integer For i = 1000 To 9999 j = Val(Mid

22、(Trim(Str(i), 2, 2) If j 2 = i Then Print i, sum = sum + i End If Next'* End *End Sub'-'題目: (事件)單擊窗體。' (響應(yīng))求一個數(shù),它除3余2,除5余3,除7余2,' 請將滿足上面條件的最小數(shù)保存到sum變量中。 輸出 sum 應(yīng)為23' 使用for.next語句完成程序'-Private Sub Form_Click() Dim sum As Integer'* Program *Dim i As IntegerFor i = 2 To

23、1000 If i Mod 3 = 2 And i Mod 5 = 3 And i Mod 7 = 2 Then Print i Exit For End IfNext isum = i'* End *End Sub五.遞推'-'題目:編寫函數(shù)fun,函數(shù)的功能是:當(dāng)x的初值為10,每年增長率為百分之一,計算多少年以后x的值能達(dá)到y(tǒng),'并顯示所需年數(shù)的值(變量名必須為n)。' 要求使用Do . Loop Until語句來實現(xiàn)。 當(dāng)y為15時, n應(yīng)該為41'-Private Function fun(y As Single) As LongDim

24、 x As Single, n As Long'*Program*x = 10n = 0Do While x < y x = x * 1.008 n = n + 1Loopfun = n'* End *End FunctionPrivate Sub Form_Load() Show Print fun(15)End Sub'-'題目:已知,猴子吃一堆桃子,每天吃桃子總數(shù)的一半多一個。到第n天時,桃子只剩一個。編寫函數(shù)fun,函數(shù)的功能是: '求出開始桃子的數(shù)量并顯示。如:' n為7時,則顯示"190"'要求使用F

25、or語句來實現(xiàn)。 要求使用Do Until . Loop語句來實現(xiàn)'-Private Function fun(n As Long) As String'*Program*x = 1i = 1Do Until i = n x = (x + 1) * 2 i = i + 1Loopfun = x'* End *End FunctionPrivate Sub Form_Load() Show Print fun(7)End Sub'-'題目:用迭代法求某個數(shù)的平方根。 已知求平方根的迭代公式如圖:'算法思想:對于一個問題的求解x,可由給定的一個初值x

26、0,根據(jù)某一' 迭代公式得到一個新的值x1,這個新值x1比初值x0更接近要求的值x;' 再以新值作為初值,即:x1x0,重新按原來的方法求x1,重復(fù)這一過和' 直到|x1-x0|<(某一給定的精度為0.00001)。此時可將x1作為問題的解。'-Private Function Fsqrt(a As Single) As Single'* Program * Dim x0 As Single, x1 As Single x0 = a / 2 '迭代初值 x1 = 0.5 * (x0 + a / x0) Do x0 = x1 '為下一

27、次迭代作準(zhǔn)備 x1 = 0.5 * (x0 + a / x0) Loop While Abs(x1 - x0) > 0.00001 Fsqrt = x1'* End *End FunctionPrivate Sub Form_Load() Show Print Fsqrt(45) ' 當(dāng)a為45 時應(yīng)輸出6.708204End Sub六.數(shù)組'-'題目:求二維數(shù)組中最大元素及其所在的行和列并將最大值存入變量Max中,將最大值的行位置存入row中,列位置存入變量column中. 輸出:'-Private Sub Form_Load() Show Di

28、m a(2, 3) As Integer Dim max As Integer, row As Integer, column As Integer a(1, 1) = 34: a(1, 2) = 34: a(1, 3) = 43: a(2, 1) = 34: a(2, 2) = 78: a(2, 3) = 12'* Program * Dim i As Integer, j As Integer max = a(1, 1): row = 1: column = 1 For i = 1 To 2 For j = 1 To 3 If a(i, j) > a(row, column)

29、 Then max = a(i, j) : row = I : column = j End If Next j Next i Print "最大元素是" max Print "在第" & row & "行," "第" & column & "列"'* End *End Sub'-'題目:應(yīng)用選擇法對數(shù)組A按升序排列'基本思想:' 1)對有n個數(shù)的序列(存放在數(shù)組a(n)中),從中選出最小的數(shù),與第1個數(shù)交換位置;'

30、 2)除第1 個數(shù)外,其余n-1個數(shù)中選最小的數(shù),與第2個數(shù)交換位置;' 3)依次類推,選擇了n-1次后,這個數(shù)列已按升序排列。' 輸出排序前后數(shù)組數(shù)據(jù),驗證結(jié)果'-Private Sub Sort(ByRef a() As Integer, n As Integer)'* Program * Dim i, j, imin, temp As Integer For i = 1 To n - 1 imin = i For j = i + 1 To n If a(imin) > a(j) Then imin = j Next j temp = a(i) a(i

31、) = a(imin) a(imin) = temp Next i'* End *End SubPrivate Sub Form_Load() Show Dim i As Integer Dim arr(10) As Integer Randomize Print "排序前:"For i = 1 To 10 arr(i) = Int(10 * Rnd + 1) : Print arr(i); Next Print Sort arr, 10 Print "排序后:" For i = 1 To 10 Print arr(i); NextEnd Sub

32、'-'題目:(事件)單擊窗體。' (響應(yīng))生成一個一維數(shù)組(10個數(shù)組元素依此為:15、23、72、43、96、23、3、65、88、17),' 寫程序求出這個數(shù)組中的最大值、最小值和平均值,并輸出在窗體上。' 將最大值,最小值,平均值分別存入變量Max,Min,Aver中 輸出Max為96,Min為3,Aver為44'-Private Sub Form_Click()Dim max, min, aver As Integer'* Program *Dim a()Dim i, s As Integera = Array(15, 23, 7

33、2, 43, 96, 23, 3, 65, 88, 17)max = a(0)min = a(0)s = 0For i = 0 To 9 If a(i) > max Then max = a(i) End If If a(i) < min Then min = a(i) End Ifs = s + a(i)Nextaver = Int(s / 10)Print max, min, aver'* End *End Sub七.雜項'-'題目:對字符串進(jìn)行加密處理。 加密過程: 將每個字母C減一序數(shù)K5,即 c=chr(Asc(c)- k),這時 "Z&

34、quot;"U", "z""u","Y""T" 當(dāng)加序數(shù)后的字母小于"A"或"a"則 c=Chr(Asc(c)-k +26) 輸出:'-Private Function EnyStr(strI As String) As String'* Program * Dim i As Integer Dim strp As String, strT As String, iA As String Dim nl As String i = 1: st

35、rp = "" nl = Len(RTrim(strI) Do While (i <= nl) strT = Mid$(strI, i, 1) '取第i個字符 If (strT >= "A" And strT <= "Z") Then iA = Asc(strT) + 5 If iA > Asc("Z") Then iA = iA - 26 strp = strp + Chr$(iA) ElseIf (strT >= "a" And strT <= &

36、quot;z") Then iA = Asc(strT) + 5 If iA > Asc("z") Then iA = iA - 26 strp = strp + Chr$(iA) Else strp = strp + strT End If i = i + 1 Loop EnyStr = strp'* End *End FunctionPrivate Sub Form_Load() Show Print EnyStr("abc def xyz")End Sub'-'題目:編寫函數(shù)fun,函數(shù)的功能是:計算兩個正整

37、數(shù)的最大公約數(shù),并顯示。'如:兩個數(shù)為15、20,結(jié)果顯示"5" ' 要求使用Do . Loop語句來實現(xiàn)。'-Private Function fun(m As Integer, n As Integer) As IntegerDim r%If m > n Then r = m: m = n: n = r'*Program*Do r = m Mod n If r = 0 Then Exit Do m = n n = rLoopfun = n'* End *End FunctionPrivate Sub Form_Load()

38、Show Print fun(15, 10)End Sub'-'題目:(事件)單擊窗體。' (響應(yīng))一球從100米高度自由落下,每次落地返回原高度的一半,求第10次落地時,共經(jīng)過多少米?并將結(jié)果在窗體上輸出。 輸出s應(yīng)為299.6094'-Private Sub Form_Click()Dim s As Single'* Program *Dim j, i As Integers = 0j = 100For i = 2 To 10 j = j / 2 s = s + 2 * jNexts = 100 + sPrint s'* End *End Sub'-'題

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論