剛開始摸到陣列,還不太會寫....
題目是說任意輸入十個數值(用inputbox)
然後由大到小排序,並且列印出來.
問過老師,他給的提示是寫兩層迴圈
\"可能\"會用到Ubound這個函數
我的想法是第一次抓陣列中最大數值,排在第一個
第二次抓剩下數值中最大的....然後一直抓到完為止
可是寫不出來><
請各位高手幫幫忙....謝謝!
2006-05-21 20:49:39 · 3 個解答 · 發問者 迷途小書僮 1 in 電腦與網際網路 ➔ 程式設計
首先脫出一個command bottom
以下是程式碼
Private Sub Command1_Click()
Dim x(1 To 10) As Integer '宣告'
For i = 1 To 10 '寫一個迴圈,輸入資料'
x(i) = Val(InputBox("輸入第" & i & "筆資料"))
Next
For i = 1 To 10
For j = i + 1 To 10 'x(i)要跟x(j)比 x(j) 一定是x(i) 的下一個開始
'可寫成For j = i To 10 只是多比了一次'
If x(i) < x(j) Then
tmp = x(i) '這一段做排序,若x(j)比x(i)大則互換位置'
x(i) = x(j)
x(j) = tmp
End If
Next
Next
For i = 1 To 10
Print x(i)
Next
End Sub
2006-05-22 13:59:36 · answer #1 · answered by 彥翔 1 · 0⤊ 0⤋
也很謝謝WJS....但是我是初學者,有一點看不懂這樣的寫法....
嗯,再次感謝你....!
2006-05-22 21:19:13 · answer #2 · answered by 迷途小書僮 1 · 0⤊ 0⤋
Private Sub Command1_Click()Dim A(1 To 10) As String, B(1 To 10) As String, I%, J%, S$, S1$, N%'先輸入10個數字For I = 1 To 10 Do S = InputBox("請輸入第" & I & "個數字") If S = "" Then Exit Sub Loop Until IsNumeric(S) A(I) = S '你的想法使用的陣列 B(I) = S '先排序後再輸出使用的陣列Next'按照你的想法從陣列中1個1個抓For I = 1 To 10 S = "": N = 0 For J = 1 To 10 If A(J) <> "" Then If S = "" Then S = A(J): N = J Else If Val(S) < Val(A(J)) Then S = A(J): N = J End If End If Next S1 = S1 & S & "," A(N) = ""NextPrint Left(S1, Len(S1) - 1)'另外1種方法,先排序後再輸出For I = 1 To 9 For J = I + 1 To 10 If Val(B(I)) < Val(B(J)) Then S = B(I): B(I) = B(J): B(J) = S NextNextPrint Join(B, ",")End Sub
2006-05-22 09:00:10 · answer #3 · answered by W.J.S. 7 · 0⤊ 0⤋