表达式不是方法-冒泡排序

时间:2018-12-11 20:01:11

标签: vb.net bubble-sort

我正在Visual Basic VB.NET中编写经典的冒泡排序,并且出现错误BC30454表达式不是一种方法。 当我开始在第一个“ 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

2 个答案:

答案 0 :(得分:2)

感谢克里斯·阿奇里奇(

我很糟糕,删除极点(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

答案 1 :(得分:1)

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

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