2013-08-07 219 views
5

因此,我在c#中做了一個非常簡單的字生成器程序,該程序運行得相當好。它根據用戶定義的長度生成一個單詞。隨機字生成器#2

該算法爲序列中的每個連續字母添加一個輔音,然後是一個元音,這並不理想,但對於基本單詞的效果不錯。

我唯一的問題是,我告訴它之前,它的權利添加一個「U」的字母序列,如果「Q」表示,但無論我做什麼它使這個詞至少1個字母太長。

我在上面的註釋中用星號標記了我的問題區域。下面是代碼:

public void WordFinder() 
{ 
    string word = null; 
    int cons; 
    int vow; 
    //counter 
    int i = 0; 
    bool isword = false; 
    Random rnd = new Random(); 
    //set a new string array of consonants 
    string[] consonant = new string[]{"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"}; 
    //set a new string array of vowels 
    string[] vowel = new string[]{"a","e","i","o","u"}; 
    while (isword == false) 
    { 
     word = null; 
     Console.WriteLine("Pick the length of a word"); 
     int num = Convert.ToInt32(Console.ReadLine()); 
     //set the counter "i" to 1 
     i = 1; 
     if (num%2 == 0) 
     { 
      while (i <= num) 
      { 
       if (num != 1) 
       { 
        // current consonant = random consonant 
        cons = rnd.Next(0, 20); 
        // get the consonant from the string array "consonant" and add it to word 
        word = word + consonant[cons]; 
        // add 1 to counter 
        i ++; 
        //* if the consonant is "q" 
        if (cons == 12) 
        { 
         // add "u" right after it 
         word = word + vowel[4]; 
         // add 1 to counter 
         i++; 
        } 
       } 
       vow = rnd.Next(0, 4); 
       word = word + vowel[vow]; 
       i ++; 
      } 
     } 
     if (num % 2 != 0) 
     { 
      while (i <= num - 1) 
      { 
       //repeat same code as done to even length 
       if (num != 1) 
       { 
        cons = rnd.Next(0, 20); 
        word = word + consonant[cons]; 
        i ++; 
        if (cons == 12) 
        { 
         word = word + vowel[4]; 
         i ++; 
        } 
       } 
       vow = rnd.Next(0, 4); 
       word = word + vowel[vow]; 
       i ++; 
      } 
      // if the length is not equal to 1 
      if (num != 1) 
      { 
       // add a consonant to the end of the word 
       cons = rnd.Next(0, 20); 
       word = word + consonant[cons]; 
      } 
      //if the length is 1 
      else if (num == 1) 
      { 
       // pick a vowel 
       vow = rnd.Next(0, 4); 
       word = word + vowel[vow]; 
      } 
     } 
     i = 1; 
     Console.WriteLine(word); 
     Console.WriteLine("Is this a word? (y/n)"); 
     string q = Console.ReadLine(); 
     q = q.ToLower(); 
     //if the answer is yes, then it is a word and end the loop 
     if (q == "y" || q == "yes") 
     { 
      isword = true; 
     } 
     //if the answer is no try the loop again 
     else if (q == "n" || q == "no") 
     { 
      isword = false; 
     } 
    } 
} 
// main method 
static void Main(string[] args) 
{ 
    Program prog = new Program(); 
    prog.WordFinder(); 
    //wait for user input 
    Console.ReadLine(); 
} 
} 
+17

該代碼中的幾乎所有註釋都沒有幫助。評論應該在那裏告訴你,代碼*不會告訴你(或者代碼中不明顯)的信息。只是說明代碼的作用並不能幫助任何人,但這隻會浪費人們的時間。 – Servy

+2

我不明白*它是什麼使它成爲一個字母(或者在「u」長*之後添加了「q」的次數)意味着你可以嘗試使用'consonant [cons] =='q' '作爲你的'if'條件,而不是你在那裏的神奇數字12 –

+2

我讀這個的方式是,每循環一次,你要加2個字母,一個輔音和一個元音。一個'Q'你總共添加了3個字母:'QU'加上一個隨機的其他元音,這裏的邏輯看起來有點不對頭 – NotMe

回答

8

我重構你的答案和一些調試後,我得到它的工作。對不起,我不能只是做一個調整來解決它。我相信它不允許以「qu」或「q」結尾。

public void WordFinder() 
{ 
    bool isWord = false; 
    Random rnd = new Random(); 
    string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" }; 
    string[] vowels = { "a", "e", "i", "o", "u" }; 


    while (isWord == false) 
    { 
     string word = ""; 

     Console.WriteLine("Pick the length of a word"); 
     int requestedLength = Convert.ToInt32(Console.ReadLine()); 

     // Generate the word in consonant/vowel pairs 
     while (word.Length < requestedLength) 
     { 
      if (requestedLength != 1) 
      { 
       // Add the consonant 
       string consonant = GetRandomLetter(rnd, consonants); 

       if (consonant == "q" && word.Length + 3 <= requestedLength) // check +3 because we'd add 3 characters in this case, the "qu" and the vowel. Change 3 to 2 to allow words that end in "qu" 
       { 
        word += "qu"; 
       } 
       else 
       { 
        while(consonant == "q") 
        { 
         // Replace an orphaned "q" 
         consonant = GetRandomLetter(rnd, consonants); 
        } 

        if (word.Length + 1 <= requestedLength) 
        { 
         // Only add a consonant if there's enough room remaining 
         word += consonant; 
        } 
       } 
      } 

      if (word.Length + 1 <= requestedLength) 
      { 
       // Only add a vowel if there's enough room remaining 
       word += GetRandomLetter(rnd, vowels); 
      } 
     } 

     Console.WriteLine(word); 
     Console.WriteLine("Is this a word? (y/n)"); 
     string q = Console.ReadLine().ToLower(); 

     if (q == "y" || q == "yes") 
     { 
      isWord = true; 
     } 
    } 
} 

private static string GetRandomLetter(Random rnd, string[] letters) 
{ 
    return letters[rnd.Next(0, letters.Length - 1)]; 
} 

編輯:但是,這仍然是相當不守規矩。如何生成隨機字符串,然後在完成後用「qu」替換「q」?

public string WordFinder2(int requestedLength) 
{ 
    Random rnd = new Random(); 
    string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" }; 
    string[] vowels = { "a", "e", "i", "o", "u" }; 

    string word = ""; 

    if (requestedLength == 1) 
    { 
     word = GetRandomLetter(rnd, vowels); 
    } 
    else 
    { 
     for (int i = 0; i < requestedLength; i+=2) 
     { 
      word += GetRandomLetter(rnd, consonants) + GetRandomLetter(rnd, vowels); 
     } 

     word = word.Replace("q", "qu").Substring(0, requestedLength); // We may generate a string longer than requested length, but it doesn't matter if cut off the excess. 
    } 

    return word; 
} 

private static string GetRandomLetter(Random rnd, string[] letters) 
{ 
    return letters[rnd.Next(0, letters.Length - 1)]; 
} 
+0

+1表示將'q'替換爲'qu'的建議。 – Bobo

+0

我很欣賞這些答案格雷格。我對c#和編程一般都比較陌生,並且一直在玩c#,並且提出了這個代碼。由於我對c#庫和類的知識極少,所以我不得不以這種粗暴而簡單的方式編寫我的代碼。我很欣賞你展示了兩種選擇,一種更接近源程序,另一種更有效。 –

1

您的問題是,因爲你正在構建的循環的方式發生。

根據長度是偶數還是奇數,使用兩個單獨的循環,並假設每個循環將添加兩個字符。但是,當遇到Q時,循環會添加3個字符,這會導致循環執行一次額外的時間,最終會生成一個額外的字符。

試試這個方法:

string GenerateWord(int length) 
    { 
     if (length < 1) // do not allow words of zero length 
      throw new ArgumentException("Length must be greater than 0"); 

     string word = string.Empty; 

     if (rand.Next() % 2 == 0) // randomly choose a vowel or consonant to start the word 
      word += cons[rand.Next(0, 20)]; 
     else 
      word += vowel[rand.Next(0, 4)]; 

     for (int i = 1; i < length; i += 2) // the counter starts at 1 to account for the initial letter 
     { // and increments by two since we append two characters per pass 
      char c = cons[rand.Next(0, 20)]; 
      char v = vowel[rand.Next(0, 4)]; 

      if (c == 'q') // append qu if the random consonant is a q 
       word += "qu"; 
      else // otherwise just append a random consant and vowel 
       word += c + v; 
     } 

     // the word may be short a letter because of the way the for loop above is constructed 
     if (word.Length < length) // we'll just append a random consonant if that's the case 
      word += cons[rand.Next(0, 20)]; 

     return word; 
    } 
+0

良好的第一遍,但是它跳過了一個元音總是跟隨'QU'的要求。此外,存在以Q. – NotMe

+0

結尾的可能性我沒有看到定義的要求。我看到OP代碼暗示了它,但我並不完全確定這是有意的。 – Michael

+0

@Michael - 我有一個答案[我刪除](http://i.stack.imgur.com/TWa5L.png)OP在哪裏做出這個要求。對困惑感到抱歉。 – Greg

0

對不起,我很晚。但是對於有問題的人來說,這是最好的和最簡單的解決方案。確保使用System.Net;

 public string RandomWord() 
     { // add a dispose to the webclient to make things go more smoothly 
     return new WebClient().DownloadString("http://setgetgo.com/randomword/get.php"); 
     }