English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

問題:輸入十個數值,利用副程式maxmin(m,n,array1()),求出其中最大的數與最小的數

2006-12-18 09:20:14 · 2 個解答 · 發問者 ? 5 in 電腦與網際網路 程式設計

請問水月:
如果用副程式要怎麼寫呢??
Private Sub Form_Activate()
Dim a(1 To 10) As Single, I As Integer
Do
I = I 1
a(I) = InputBox("請輸入第" & Str(I) & "個數")
Loop Until I = 10
End Sub
這是小弟寫的,不過副程式不會寫
是否可以指點一下呢??
還有如果有錯還請指正
多謝....

2006-12-19 11:19:26 · update #1

2 個解答

將下列程式寫入試試看。
Private Sub Form_activate()
Dim num(1 To 10) As Single
num(1) = Val(InputBox("請輸入第一個數值"))
num(2) = Val(InputBox("請輸入第二個數值"))
num(3) = Val(InputBox("請輸入第三個數值"))
num(4) = Val(InputBox("請輸入第四個數值"))
num(5) = Val(InputBox("請輸入第五個數值"))
num(6) = Val(InputBox("請輸入第六個數值"))
num(7) = Val(InputBox("請輸入第七個數值"))
num(8) = Val(InputBox("請輸入第八個數值"))
num(9) = Val(InputBox("請輸入第九個數值"))
num(10) = Val(InputBox("請輸入第十個數值"))
For i = 1 To 10
Print num(i)
Next i
max_num = Max(10, num())
min_num = Min(10, num())
Print
Print "最大數 ="; max_num
Print "最小數 ="; min_num

End Sub

Private Function Max(ByVal m As Integer, array1() As Single)
Max = array1(1)
For j = 2 To m
If array1(j) > Max Then Max = array1(j)
Next j
End Function

Private Function Min(ByVal n As Integer, array1() As Single)
Min = array1(1)
For j = 2 To n
If array1(j) < Min Then Min = array1(j)
Next j
End Function

2006-12-19 19:41:30 補充:
我最下面寫的那二個Function那就是一個副程式了。將一段具有某種特定功能的敘述區段或是重複出現的程式區段單獨抽出來,編寫成一個獨立的程式單元,並給予特定名稱,以方便其他程式呼叫使用,我們將這類的程式單元稱為「副程式」,詳細的說明你可以去參考書本,我用的是Function...End Function程序,下面那位高手用的是Sub...End Sub程序,他寫的比我高竿多了,你也可以參考看看。
你輸入數值的程式和我寫的是差不多,只是我是一個一個輸入,你用迴圈去讓使用者輸入十個數後離開也是可以的,後面的副程式只要套進你的程式功能都是一樣的。

2006-12-18 11:46:05 · answer #1 · answered by 水月 6 · 0 0


Private Sub Command1_Click()
Dim A(1 To 10) As Single, I As Integer, S As String

For I = 1 To 10
Do
S = InputBox("請輸入第" & I & "個數值")
If S = "" Then Exit Sub
Loop Until IsNumeric(S)
A(I) = S
Next
Dim M As Single, N As Single

Call MaxMin(M, N, A)
Print "最大值 ="; M
Print "最小值 ="; N
End Sub
 
Sub MaxMin(M As Single, N As Single, Array1() As Single)
Dim I As Integer, J As Integer, T As Single

For I = 1 To 9
For J = I To 10
If Array1(I) > Array1(J) Then
T = Array1(I): Array1(I) = Array1(J): Array1(J) = T
End If
Next
Next
M = Array1(10): N = Array1(1)
End Sub

2006-12-18 12:01:15 · answer #2 · answered by W.J.S. 7 · 0 0

fedest.com, questions and answers