2015-05-18 25 views
-4

我需要隨機或洗牌文本文件中的所有單詞。如何將文本文件中的所有單詞進行混洗或隨機化?

例如在我的文本文件我有以下的話

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 
+4

你有什麼試過的?自己嘗試一下,如果你遇到困難,請回答一個具體問題。這不是一個代碼寫入服務。 –

+0

隨機化任何事情都很容易。首先用單詞分解一個列表/數組。接下來的步驟應該非常明顯。 – deathismyfriend

+0

我從來沒有要求爲我編碼!我在這裏引用http://stackoverflow.com/questions/13499409/how-i-can-randomize-the-content-of-a-text-file – softfx

回答

0
  1. 提取每個單獨字作爲一個字符串並把它添加到一個字符串數組。

  2. 亂序你的字符串數組。

  3. 以新的洗牌順序打印數組中的單詞。

相關問題