我一直在研究以下代码,整个目标是获取2个文件的内容,然后按从最低到最大的顺序对其进行排序并输出。我一直得到的错误是数组中的数字确实排序,但没有从最低到最大的顺序排列。
我有三个分别标记为Input1.txt和Input2.txt的文本文件和一个输出文本文件。
Input1中的内容如下: 5 3 2 1个 9 12 34
Input2中的内容如下: 4 13 16 23 56 -7
这是我到目前为止的代码:
Imports System.IO
Public Class Form1
'Calling up a filepath for saving the users input plus altered input [file names]
Public filePath As String = "Input1.TXT"
Public filePath2 As String = "Input2.TXT"
Public outputFile As String = "Output.txt"
Public objReader As New System.IO.StreamReader(filePath)
Public objReader1 As New System.IO.StreamReader(filePath2)
Dim line As String
'Temp Variables
Dim TempS As String
Dim TempY As String
Dim iPass As Integer
Dim iTemp As Integer
'Declaring variable for how many numbers there are in TextFileNumbes.txt (I have listed 6 numbers in this case)
Dim numbers(100) As String
'Declaring variable for creating a counter for reading all the numbers in TextFileNumbers.txt
Dim i As Integer = 0
'Event - To Load Name From TextFile
Public Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
If objReader.Peek() <> -1 Then
'Read one line from the input textfile
TempS = objReader.ReadLine()
'Declaring code for placing the numbers into an array
numbers(i) = TempS
'Add name read to listbox
ListBox1.Items.Add(TempS)
'Add numbers to the listBox titled "Data Combined From Both Files"
ListBox2.Items.Add(numbers(i))
'Increment Array
i += 1
Else
'Prompts User End of File Has Been Reached
MessageBox.Show("End of File Has Been Reached!")
End If
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If objReader1.Peek() <> -1 Then
'Read one line from the input textfile
TempY = objReader1.ReadLine()
'Declaring code for placing the numbers into an array
numbers(i) = TempY
'Add name read to listbox
lstBoxInputFile.Items.Add(TempY)
'Declaring code for the numbers/data stored in TextFileNumbers to be passed onto the OriginalData listbox
ListBox2.Items.Add(numbers(i))
'Increment array counter
i += 1
Else
'If file not located, prompts user that the file isn't there
MessageBox.Show("End of File Has Been Reached!")
End If
End Sub
'Event - save the user input plus altered text
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
End Sub
'Event to clear output textfile
Public Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
For iPass = 1 To numbers.Length - 1
For i = 0 To numbers.Length - 2
If numbers(i) > numbers(i + 1) Then
iTemp = numbers(i)
numbers(i) = numbers(i + 1)
numbers(i + 1) = iTemp
End If
Next i
Next iPass
Dim sortOut As String
For i = 0 To numbers.Length - 1
If Not String.IsNullOrEmpty(numbers(i)) Then
ListBox3.Items.Add(numbers(i))
End If
Next
MessageBox.Show("The Data has been sorted!")
End Sub
End Class
我一直得到的输出如下:-7,1,12,13,16,16,2,23,3,34,4,5,56,9当它应该是-7,1,2 ,3、4、5、9、12、13、16、23、34、56
答案 0 :(得分:1)
您可能遇到的最大问题是走路之前可能要跑步。
您正在将数据从文本文件读取到字符串数组中,然后尝试比较字符串值。这条线..
If numbers(i) > numbers(i + 1) Then
正在比较字符串,而不是整数。当您比较一个字符串以查看哪个更大时,实际上发生的是第一个字符(不是数字,它可以是“ a”或“ /”或“ 3”),然后比较字符代码的值。然后确定哪个值更高,然后针对您要比较的每个字符串中的所有字符执行此操作。同样,如果字符串中的字符数不同,则字符数最少的字符串将被认为小于较长的字符串。有道理。
所以..在数字列表中查看特定的三元组“ 16”,“ 2”和“ 23”
比较代码有时会比较“ 16”和“ 2”,并且由于第一个字符是“ 1”和“ 2”,因此“ 16”小于“ 2”。
尽管第一个字符相同,但“ 2”和“ 23”之间的比较略有不同,这是因为“ 2”比“ 23”短,因此“ 2”小于“ 23” >
要正确比较数字,必须将每个字符串值转换或解析为数字值,并将其存储为数字-而不是字符串。看看这个,并与您的代码进行比较。.
Public Class Form1
'Calling up a filepath for saving the users input plus altered input [file names]
Public filePath As String = "Input1.TXT"
Public filePath2 As String = "Input2.TXT"
Public outputFile As String = "Output.txt"
Public objReader As New System.IO.StreamReader(filePath)
Public objReader1 As New System.IO.StreamReader(filePath2)
'Temp Variables
Dim TempS As Integer
Dim TempY As Integer
Dim iPass As Integer
Dim iTemp As Integer
'Declaring variable for how many numbers there are in TextFileNumbes.txt (I have listed 6 numbers in this case)
Dim numbers(100) As Integer
'Declaring variable for creating a counter for reading all the numbers in TextFileNumbers.txt
Dim i As Integer = 0
'Event - To Load Name From TextFile
Public Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
If objReader.Peek() <> -1 Then
'Read one line from the input textfile
TempS = Integer.Parse(objReader.ReadLine())
'Declaring code for placing the numbers into an array
numbers(i) = TempS
'Add name read to listbox
ListBox1.Items.Add(TempS)
'Add numbers to the listBox titled "Data Combined From Both Files"
ListBox2.Items.Add(numbers(i))
'Increment Array
i += 1
Else
'Prompts User End of File Has Been Reached
MessageBox.Show("End of File Has Been Reached!")
End If
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If objReader1.Peek() <> -1 Then
'Read one line from the input textfile
TempY = Integer.Parse(objReader1.ReadLine())
'Declaring code for placing the numbers into an array
numbers(i) = TempY
'Add name read to listbox
lstBoxInputFile.Items.Add(TempY.ToString)
'Declaring code for the numbers/data stored in TextFileNumbers to be passed onto the OriginalData listbox
ListBox2.Items.Add(numbers(i).ToString)
'Increment array counter
i += 1
Else
'If file not located, prompts user that the file isn't there
MessageBox.Show("End of File Has Been Reached!")
End If
End Sub
'Event - save the user input plus altered text
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
End Sub
'Event to clear output textfile
Public Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
For iPass = 1 To numbers.Length - 1
For i = 0 To numbers.Length - 2
If numbers(i) > numbers(i + 1) Then
iTemp = numbers(i)
numbers(i) = numbers(i + 1)
numbers(i + 1) = iTemp
End If
Next i
Next iPass
For i = 0 To numbers.Length - 1
ListBox3.Items.Add(numbers(i).ToString)
Next
MessageBox.Show("The Data has been sorted!")
End Sub
End Class
所有临时变量现在都定义为整数。现在,当VB.Net比较实际数字而不是字符串时,1
现在小于16
。瞧!
在我的示例中,声明了变量line
和sortout
的行已删除,因为您的代码未使用它们。
您还将注意到,我在多个位置添加了.ToString
。严格来说,这不是必需的,因为VB会在后台愉快地将Integers转换为Strings,但是我添加了它们是为了让您知道它发生了。