因此,我有一個VB.NET控制檯程序,用戶可以輸入一個句子,然後句子被每個單詞分割並給出值。程序然後檢查任何重複的單詞,並將重複單詞出現的位置與第一個單詞相加。VB.NET:從文本文件中解壓縮/重寫一個句子編輯:w/code
例如(如我明白,可能沒有被很好的解釋): - 用戶輸入「我的名字是你好」 - 計劃拆分句和指定值作爲這樣的:「你好1,我的2,名稱3 ,是4,你好5「(每個單詞和數字對在數組中) - 程序檢測到重複的單詞並將數字添加到第一個,如:」你好1 5,我的2,名稱3,是4「 - 單詞和數字寫入文本文件
現在需要從文本文件中讀取單詞。我正在使用LineInput(1)/ File Open函數從文本文件中讀取數據,並將文本文件的每一行分隔一個空格,以使數字和單詞分開。但是,將單詞作爲句子打印回控制檯時,具有重複項的單詞只會出現一次,因爲它們只在文本文件中出現一次,但只有兩個數字。
我明白這個問題,但無法找到修復它的方法,雖然它可能只是我需要以不同的方式/格式寫入文本文件。對於任何能夠幫助/給我提示的人都會很感激。
Module Module1
Dim i As Integer = 0
Dim textInput As String
Dim finalTextInput(1) As String
Sub Main()
Console.WriteLine("Please enter your sentence(s)")
Dim sentence As String = Console.ReadLine()
Dim sentenceSplit() As String = sentence.Split(" ")
For Each element In sentenceSplit
sentenceSplit(i) = element & " " & i
i += 1
Next
Dim output As New Dictionary(Of String, String)
For Each current In sentenceSplit
' split current input
Dim currentSplited = current.Split(" ")
Dim word = currentSplited(0)
Dim trailingNumbers = currentSplited(1)
' if it already exists
If output.ContainsKey(word) Then
' add trailing numbers
output(word) = output(word) & " " & trailingNumbers
Else
' new input
output.Add(word, trailingNumbers)
End If
Next
' create new array from dictionary
Dim newArray = output.Select(Function(x) x.Key & " " & x.Value).ToArray()
FileOpen(1, "compressed.txt", OpenMode.Output)
For Each element In newArray
PrintLine(1, element)
Next
FileClose(1)
Read()
End Sub
Sub Read()
Console.WriteLine("Would you like to decompress your sentences? Y/N")
Dim input As String = Console.ReadLine()
If input.ToLower() = "y" Then
FileOpen(1, "compressed.txt", OpenMode.Input)
Do Until EOF(1)
textInput = LineInput(1)
finalTextInput = textInput.Split(" ")
Console.Write(finalTextInput(0) & " ")
Loop
FileClose(1)
ElseIf input.Tolower() = "n" Then
Console.WriteLine("Thank you for using our system")
Threading.Thread.Sleep(2000)
End
Else
Console.WriteLine("Please choose one of the options")
Threading.Thread.Sleep(1500)
Console.Clear()
Read()
End If
Console.ReadKey()
End Sub
前端模塊
如果你想幫助調試你的代碼,你應該在你的代碼中包含你的代碼。 – Mark
@Mark好的,謝謝我通常這樣做,但我認爲這個解釋已經足夠,因爲我知道問題出在哪裏,只需要一個修復 –