我怎样才能洗牌或随机化文本文件中的所有单词?
how i can shuffle or randomize all words in textfile?
我需要随机化或随机排列文本文件中的所有单词。
例如在我的文本文件中我有以下单词
hello world i have good one
today is good day
it good to see you
all that kind thing world
boy man women girl
在洗牌或随机化后我想得到这样的结果
is see kind boy today
world all hello women
man that to you day
girl it i one good
have thing world
我在 vb.net 中可以做什么?提前致谢
已解决
模块模块 1
Sub Main()
Dim lines() As String = IO.File.ReadAllLines("C:\Users\Username\Desktop\nice.txt")
Dim outputData As String = RandomizeWords(lines)
IO.File.WriteAllText("C:\Users\Username\Desktop\nice123.txt", outputData)
Console.ReadLine()
End Sub
Private Function RandomizeWords(lines() As String) As String
Dim sb As New StringBuilder()
Dim newList As New List(Of String)
For Each line In lines
Dim orderedWords() As String = line.Split(" "c)
Dim randomizedWords = From n In orderedWords Order By Guid.NewGuid() Select n
newList.AddRange(randomizedWords)
Next
Dim wordCount As Integer = 0
For i = 0 To newList.Count - 1
Dim randomIndex As Integer = New Random().Next(0, newList.Count - 1)
If wordCount = 4 Then
sb.AppendLine(newList(randomIndex))
wordCount = 0
Else
sb.Append(newList(randomIndex) & " ")
End If
newList.RemoveAt(randomIndex)
wordCount += 1
Next
Return sb.ToString()
End Function
End Module
将每个单独的单词提取为字符串并将其添加到字符串数组中。
打乱你的字符串数组。
以新的打乱顺序打印出数组中的单词。
我需要随机化或随机排列文本文件中的所有单词。
例如在我的文本文件中我有以下单词
hello world i have good one
today is good day
it good to see you
all that kind thing world
boy man women girl
在洗牌或随机化后我想得到这样的结果
is see kind boy today
world all hello women
man that to you day
girl it i one good
have thing world
我在 vb.net 中可以做什么?提前致谢
已解决 模块模块 1
Sub Main()
Dim lines() As String = IO.File.ReadAllLines("C:\Users\Username\Desktop\nice.txt")
Dim outputData As String = RandomizeWords(lines)
IO.File.WriteAllText("C:\Users\Username\Desktop\nice123.txt", outputData)
Console.ReadLine()
End Sub
Private Function RandomizeWords(lines() As String) As String
Dim sb As New StringBuilder()
Dim newList As New List(Of String)
For Each line In lines
Dim orderedWords() As String = line.Split(" "c)
Dim randomizedWords = From n In orderedWords Order By Guid.NewGuid() Select n
newList.AddRange(randomizedWords)
Next
Dim wordCount As Integer = 0
For i = 0 To newList.Count - 1
Dim randomIndex As Integer = New Random().Next(0, newList.Count - 1)
If wordCount = 4 Then
sb.AppendLine(newList(randomIndex))
wordCount = 0
Else
sb.Append(newList(randomIndex) & " ")
End If
newList.RemoveAt(randomIndex)
wordCount += 1
Next
Return sb.ToString()
End Function
End Module
将每个单独的单词提取为字符串并将其添加到字符串数组中。
打乱你的字符串数组。
以新的打乱顺序打印出数组中的单词。