代码之家  ›  专栏  ›  技术社区  ›  J Wilson

数组vb中的交换位置。网

  •  0
  • J Wilson  · 技术社区  · 8 年前

    我试图编写一个程序,它有两个部分,这取决于按下了两个按钮中的哪一个。

    第一部分是正在工作的位,用户按下第一个标记为“unsort”的按钮,这会触发一个循环,其中显示一个输入框,要求输入8次随机数。这8个数字存储在一个数组中。

    然而,这是我正在努力解决的第二部分;第二个按钮标记为排序,应该输出用户刚刚使用第一个按钮输入的数字,从最小到最大。我知道这里必须使用冒泡排序,也必须使用循环中的循环,但是我不理解这些循环的内容。由于我的原始帖子,我编辑了帖子,在我之前坚持使用的循环中包含了一些代码,但是它仍然没有生成所需的输出(所有数字都按顺序),而只是以看似随机的顺序输出数字

    代码发布如下,带有注释:

    Public Class BubbleSort1
        Dim Bubble(8) As Integer
        Dim UnsortedList As String
        Dim n As Integer
        Dim SortedList As String
        Dim temp As String
    
    
        Private Sub btnUnsort_Click(sender As Object, e As EventArgs) Handles btnUnsort.Click
            n = 8 ' number off values on array
            For i = 1 To n ' when i is between 1 and size of array
                Bubble(i) = InputBox("Enter Number") ' User inputs a number
                UnsortedList = UnsortedList & " " & Bubble(i) & vbNewLine ' number is added to the unsorted list variable
            Next i
            lblUnsort.Text = UnsortedList ' outputs the array
        End Sub
    
        Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
    
            For i = 1 To n - 1 ' When i is between 1 and the array size - 1 (8-1):
                For j = 1 To n - 1 ' Second loop - when j is between 1 and the array size - 1 (8-1):
                    If Bubble(j) > Bubble(j + 1) Then ' if bubble value j is greater than value j - 1:
                        temp = Bubble(j)
                        Bubble(j) = Bubble(j + 1) ' These lines are supost to order the numbers but aren'r currently doing so
                        Bubble(j + 1) = temp
                        SortedList = SortedList & Bubble(j) & vbNewLine ' Adding the number in order to a variable 
                    End If
                Next j
            Next i
    
            lblSort.Text = SortedList ' outputting the ordered numbers
    
        End Sub
    End Class
    

    正如代码中所指出的,这段代码中对数字进行排序的部分只是将它们按随机顺序排列,而不是实际对它们进行排序。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Andrew Morton    8 年前

    使用更新的代码(现在包括数组元素的交换),您正在构建显示排序数组的字符串:它将显示工作,而不是最终结果。

    一旦数组按顺序排列,您只需构建字符串:

    Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
    
        ' Bubble sort the array...
        For i = 1 To n - 1 ' When i is between 1 and the array size - 1 (8-1):
            For j = 1 To n - 1 ' Second loop - when j is between 1 and the array size - 1 (8-1):
                If Bubble(j) > Bubble(j + 1) Then ' if bubble value j is greater than value j - 1:
                    temp = Bubble(j)
                    Bubble(j) = Bubble(j + 1)
                    Bubble(j + 1) = temp
                End If
            Next j
        Next i
    
        'lblSort.Text = String.Join(vbNewLine, Bubble.Skip(1)) ' an easy one-liner
    
        ' Create a string to show the sorted array...
        SortedList = "" ' clear it out in case it was used previously
        For i = 1 To n
            SortedList = SortedList & Bubble(i).ToString()
            If i < n Then ' only add a newline if it isn't the last element
                SortedList = SortedList & vbNewLine
            End If
        Next
    
        lblSort.Text = SortedList
    
    End Sub
    

    我把 .ToString() 在那里,您可以明确地将输入字符串转换为数字;严格来说 & 运算符将其参数转换为字符串,但我更喜欢在代码中明确表示。


    正如您的代码所示,存在从输入(一串数字)到整数(数组元素的类型)的隐式转换。虽然这看起来很方便,但如果VB为您猜测了错误的转换,这可能是一个问题。如果变量类型不匹配,有一种方法可以告诉它:put Option Strict On 作为第一行,它甚至会给你建议需要做些什么来纠正它。

        2
  •  0
  •   David    8 年前

    如果要提示用户输入,则首先需要使用类似NumericUpDown的控件获取数值,或者需要使用将字符串值转换为整数值 Integer.TryParse . 此外,请记住VB中的数组。Net有一个基于0的索引,因此它们从0开始,而不是从1开始。

    在冒泡排序算法方面,您将需要一个嵌套循环,就像您使用的一样 一、 j ,只有内部嵌套循环( j

    这是我创建的一个控制台应用程序示例,它不会提示用户输入随机值,而是简单地获取随机值的集合,然后执行冒泡排序:

    Private Function BubbleSort(ByVal values() As Integer) As Integer()
        'Declare placeholder variables to use in the iterations
        Dim temp As Integer
    
        For outterIndex As Integer = 0 To values.Length - 1
            For innerIndex As Integer = 0 To values.Length - 2
                If values(innerIndex) > values(innerIndex + 1) Then
                    temp = values(innerIndex + 1)
                    values(innerIndex + 1) = values(innerIndex)
                    values(innerIndex) = temp
                End If
            Next
        Next
    
        Return values
    End Function
    
    Private r As New Random()
    Private Function RandomNumbers(ByVal range As Integer) As Integer()
        'Throw an exception if the value is less than 1
        If range < 1 Then Throw New ArgumentOutOfRangeException("The range cannot be less than 1")
    
        'Return a collection of random numbers
        Return Enumerable.Range(1, range).Select(Function(i) r.Next()).ToArray()
    End Function
    

    小提琴: Live Demo