學號尾碼4或9者,試寫依程式完成以下需求)
1 可以查詢費氏數列第n項的值,費氏數列的第1項及第2項均為1
,第n項為第n-2項加第n-1項的和
2 程式須加輸入錯誤的處理
3 若查詢成功,伊所查詢的項數附帶印出規則圖形
2007-01-06 08:08:11 · 2 個解答 · 發問者 伯爵 1 in 電腦與網際網路 ➔ 程式設計
請在表單上建立二個控制項,一個是text1,一個是command1。
接著建立以下程式碼即可:
Private Sub Command1_Click()
MsgBox GetFab(Text1.Text)
MsgBox "參考位置為" & Text1.Text & vbNewLine & GetMultipleTable(Text1.Text)
End Sub
Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub
Private Sub Text1_Change()
If Text1.Text = "0" Then Text1.Text = ""
End Sub
Public Function GetFab(n As Integer) As Variant
Dim Retn As Integer
If n = 1 Or n = 2 Then
Retn = 1
ElseIf n > 2 Then
Retn = GetFab(n - 1) + GetFab(n - 2)
End If
GetFab = Retn
End Function
Public Function GetMultipleTable(n As Integer) As String
Dim i As Integer, j As Integer
Dim s As String
If n > 9 Then n = 9
s = ""
For i = 1 To n
For j = 1 To n
s = s & i & "*" & j & "=" & (i * j)
If j <> n Then s = s & Space(1) Else s = s & vbNewLine
Next
Next
GetMultipleTable = s
End Function
2007-01-06 22:05:07 · answer #1 · answered by yoo 3 · 0⤊ 0⤋
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer
Dim n As Integer, a As Integer, b As Integer
Dim buf As String
a = 1: b = 1
Do
k = 1
buf = InputBox("請輸入欲查詢的項數: ")
If Not IsNumeric(buf) Then
MsgBox "項數必須是整數啦."
k = 0
ElseIf (Val(buf) < 3) Then
MsgBox "@!!!項數至少為3以上,重來啦!"
k = 0
End If
Loop While k = 0
n = Val(buf)
For i = 3 To n
k = a + b
a = b
b = k
Next i
Print 費氏數列第 " & n & " 項 = " & k
For i = n To 1 Step -1
Print Space(n - i) & String(2 * i, "H")
Next i
End Sub
如果有問題, 請來函討論. 不然, 我可能會錯失你再補充的疑點
2007-01-06 08:33:57 · answer #2 · answered by JJ 7 · 0⤊ 0⤋