表达式不是方法-Bubble排序



我正在Visual Basic VB.NET中编写一个经典的冒泡排序,但得到一个错误BC30454 Expression不是一个方法。当我开始对第一个"For"中插入的数字进行排序时,代码遇到了问题。我将Sub更改为Public,并再次检查方法是否正确键入,但似乎没有任何帮助。我错过了什么?

Sub EE14()
Dim j As Long, p As Long, n As Long, i As Long, Pole(100) As Long
Dim s As String
n = 0
Do
i = Val(InputBox("Write a number:"))
If i <> 0 Then
n = n + 1
Pole(n) = i
End If
Loop Until i = 0
For j = 0 To (n - 1)
For i = 0 To (n - 1)
If Pole(i) > Pole(i + 1) Then
p = Pole(i)
Pole(i) = Pole(i + 1)
Pole(i + 1)
Pole(i + 1) = p
End If
Next
Next
For i = 0 To n
s = s & vbCrLf & Str(Pole(i))
Next
MsgBox(s)
End Sub

感谢的Chris Akridge

糟糕的是,通过移除极点(i+1(,没有问题。代码现在工作正常。

Sub EE14()
Dim j As Long, p As Long, n As Long, i As Long, Pole(100) As Long
Dim s As String
n = 0
Do                 
i = Val(InputBox("Add Number:"))
If i <> 0 Then
n = n + 1
Pole(n) = i
End If
Loop Until i = 0
For j = 0 To (n - 1) 
For i = 0 To (n - 1)
If Pole(i) > Pole(i + 1) Then
p = Pole(i)
Pole(i) = Pole(i + 1)
Pole(i + 1) = p
End If
Next
Next
For i = 0 To n
s = s & vbCrLf & Str(Pole(i))
Next
MsgBox(s)
End Sub

一些更改,其中之一是删除了允许的输入数量。将排序移动到其自己的方法。

Sub EE14()
Dim i As Integer, n As Integer
Dim Pole As New List(Of Long)
Dim inp As Long
Dim s As String
Do
s = InputBox("Add Number:")
If Long.TryParse(s, inp) AndAlso inp > 0L Then
Pole.Add(inp)
End If
Loop Until inp = 0L
SortListOfLongs(Pole)
Dim sb As New System.Text.StringBuilder
For i = 0 To Pole.Count - 1
sb.AppendLine(Pole(i).ToString)
Next
MsgBox(sb.ToString)
End Sub
Private Sub SortListOfLongs(LoL As List(Of Long))
For j As Integer = 0 To LoL.Count - 1
For i As Integer = 0 To LoL.Count - 2
If LoL(i) > LoL(i + 1) Then
Dim p As Long = LoL(i)
LoL(i) = LoL(i + 1)
LoL(i + 1) = p
End If
Next
Next
End Sub

最新更新