求助 如何在一个排好大小的10个随机函数中加入令一个数 也按照从小到大排序 在线等 跪求
[code=VB][/code]Dim a(12) As Integer
Dim s As Integer
Private Sub Command1_Click()
For i = 1 To 10
a(i) = Int(Rnd * 100 + 1)
Print a(i);
Next
Print
s = Int(Rnd * 100 + 1)
Print s;
Print
End Sub
Private Sub Command2_Click()
For i = 1 To 9
For j = i + 1 To 10
If a(i) > a(j) Then
c = a(i)
a(i) = a(j)
a(j) = c
End If
Next
Next
For i = 1 To 10
If s > a(i) And s < a(i + 1) Then
End If
Next
For i = 1 To 11
Print a(i);
Next
End Sub
[解决办法]
既然你要求在已有代码基础上改,那我就不说别的了。
看看这个吧:
Option ExplicitDim a(12) As IntegerDim s As IntegerPrivate Sub Command1_Click() Dim i& For i = 1 To 10 a(i) = Int(Rnd * 100 + 1) Print a(i); Next Print s = Int(Rnd * 100 + 1) Print s; PrintEnd SubPrivate Sub Command2_Click() Dim i&, j&, c% For i = 1 To 9 For j = i + 1 To 10 If a(i) > a(j) Then c = a(i) a(i) = a(j) a(j) = c End If Next Next For i = 1 To 10 'If s > a(i) And s < a(i + 1) Then If (s < a(i)) Then '因为你是从小到大排序的 For j = 11 To i + 1 Step -1 a(j) = a(j - 1) Next a(i) = s Exit For End If Next For i = 1 To 11 Print a(i); NextEnd Sub