我怎么可以随机播放或随机化文本文件中的所有单词?

时间:2015-05-18 09:13:46

标签: c# vb.net random text-files

我需要随机化或随机播放文本文件中的所有单词。

例如在我的文本文件中我有以下单词

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

1 个答案:

答案 0 :(得分:0)

  1. 将每个单独的单词解压缩为字符串,并将其添加到字符串数组中。

  2. 随机播放字符串数组。

  3. 以新的洗牌顺序打印出数组中的单词。

相关问题