2016-11-05 83 views
0

我一直坚持这一段时间,现在我似乎无法得到它的工作。我希望我的程序能够从“Words”数组中选择一个随机单词(用户先前添加的数组内容),并允许用户输入另一个单词并查看它是否与程序选择的随机选择的单词相匹配阵列。如果单词匹配一条消息将被输出,但如果不是,则还将输出一条消息,但是系统应该向用户指示他们已经输入的任何字母是否在随机字符串中。我知道这是相当多的,但我已经被困住了很久,哈哈,谢谢! 这是我一直在使用的代码的一部分,有点简化。将输入的字符串匹配到一个随机字符串

私人无效btnGuess_Click(对象发件人,EventArgs的) {

 string guess = txtGuess.Text; 

     string[] words = new string[6]; 
     lstWords.Items.Add(txtEnterWord.Text); 

     Random rand = new Random(); 

     for (int i = 0; i < words.Length; i++) 
     { 
      words[i] = rand.ToString(); 
     } 
     if (String.Equals(guess, rand)) 
     { 
      MessageBox.Show("Congratulations you have won! Your words are a match"); 
     } 
     else 
     { 
      MessageBox.Show("Sorry but your words are not a match, try again"); 
     } 
    } 

回答

0

你必须

  • 与实际的话初始化words阵列

  • 有挑选的方法从words排列一个随机单词

像如下:

private void btnGuess_Click(object sender, EventArgs e) 
    { 
     string guess = txtGuess.Text; 

     string[] words = new string[6] { "word1", "word2", "word3", "word4", "word5", "word6" }; //<--| initialize words array 
     lstWords.Items.Add(txtEnterWord.Text); //<--| what is that for? 

     string randWord = GetRandomWord(words); //<--| get rabdom word from words array 

     if (String.Equals(guess, randWord)) 
     { 
      MessageBox.Show("Congratulations you have won! Your words are a match"); 
     } 
     else 
     { 
      MessageBox.Show("Sorry but your words are not a match, try again"); 
     } 

    } 

    static private string GetRandomWord(string[] words) 
    { 
     Random rnd = new Random(); 
     return words[rnd.Next(0, words.Length)].ToString(); 
    } 
+0

这里可能是好的,但总的来说,为每个需要的随机数字创建一个新的Random(随机)实例是一个坏主意。 – Jens

+0

嗨,感谢您的回复,但我需要的方式是每次输入6个单词。这6个单词每次都会有所不同,这就是为什么我没有将数组中的每个单词都设置为常量。 – JordonG

+0

@JordonG,我的代码主要显示如何_“...能够从”Words“数组中选择一个随机单词”_“。我仅仅为了测试目的而用常量初始化了“单词”。但你可以初始化它,因为你需要 – user3598756

0
Random rand = new Random() 

    for (int i = 0; i < words.Length; i++) 
    { 
     words[i] = rand.ToString(); 
    } 

在这个循环中你的阵列中分配rand.ToString()的每一个元件的输出。如果在循环之后查看数组,则每个元素都将是“System.Random”,因为在Random类型的对象上调用ToString方法将以字符串形式返回对象的类型。

当你创建一个新的Random对象时,你正在创建一个对象。一个可以返回随机数的对象。你不会创建一个随机数字。

你想要从数组中选择一个字符串,这就是你如何才能把它弄出来。

string thirdWordFromMyArrayOfWords = words[3]; 

要获取用随机的随机数,这将是你的话元素的范围内:

int randomNumberWithinTheRangeOfMyArray = rand.Next(0,words.Length-1) 

你需要减去之一,因为数组有6个元素(words.Length = 6)但它从0,1,2,3,4,5开始计数,所以如果你试图引用单词[6],你会抛出异常。

int randomNumberWithinTheRangeOfMyArray = rand.Next(0,words.Length-1); 
string randomWordFromArray = words[randomNumberWithinTheRangeOfMyArray]; 
if (String.Equals(guess, randomWordFromArray)) 

,它可以进一步凝聚

if (String.Equals(guess, words[rand.Next(0,words.Length-1)])) 

别的东西要考虑的是,因为每一个点击按钮时,你会得到一个新的随机数,因此时间的,要求一个新的随机数一个新的随机单词出阵列。

+0

感谢您的回复!我要用这种方式来尝试,这意味着在整个项目中,生病不得不采取完全不同的方法。我让你知道我是怎么得到的! – JordonG

0
using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace RandomWords 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      string[] words = new[] { "csharp", "Stack", "overflow", "microsoft", "word5", "Coding"}; 
      Random rnum = new Random(); 
      string input = null; 
      while (input != "end") { 
       int intnum = rnum.Next(0, words.Length); 
       Console.WriteLine("Guess a word or enter end to exit"); 
       input = Console.ReadLine(); 
       List<string> l = words.Where(x => x == input).ToList<string>(); 
       if (l.Count != 0) { Console.WriteLine("Congratulations you have won! Your words are a match"); } else { Console.WriteLine("Sorry but your words are not a match, try again"); } 
      } 

     } 
    } 
}