如何将随机选择的单词存储为单词?

时间:2015-07-23 21:51:29

标签: c# random

所以我正在制作一个刽子手计划。它将从126个不同单词的列表中拉出并随机选择其中一个单词。它还会输出一个" _"对于每个角色。

但是现在,当我试图确定“正确的猜测”时,对于这个词,我无法弄清楚如何实际存储随机选择的单词的值。听起来很有趣,但你有什么。大声笑。

String[] myWordArrays = File.ReadAllLines("WordList.txt");
        Random randomWord = new Random();
        //int lineCount = File.ReadLines("WordList.txt").Count();            
        int activeWord = randomWord.Next(0, myWordArrays.Length);
        string userSelection = "";

        Console.WriteLine("Are you Ready to play Hangman? yes/no: ");

        userSelection = Console.ReadLine();
            if(userSelection == "yes")
            {

                foreach(char letter in myWordArrays[activeWord])
                {

                    //the console.write(activeword) only shows the line number o.0. 
                    //when I try to print out .Write(myWordArrays) it shows a 
                    //System.(something) on the screen. ugh.

                    Console.Write(activeWord);
                    Console.Write("_ ");

                }

我相信这是需要引用的所有代码,因为它实际上是处理随机字选择的唯一代码。我对此感到非常困惑。我试图将不同的东西设置为活跃的词语,我无法想出任何合乎逻辑的方式来使用“foreach”#e;'同时将每个字母放入一个数组中(当我准备在用户猜出该字母时搜索每个字母时,这将是有益的)。

3 个答案:

答案 0 :(得分:2)

表示

的行
int activeWord = randomWord.Next(0, myWordArrays.Length);

将其更改为

String activeWord = myWordArrays[randomWord.Next(0, myWordArrays.Length)];

并将foreach更改为

foreach(char letter in activeWord)

并且您不需要让foreach将单词放入char数组中。当你需要一个char数组时,你可以说

activeWord.toCharArray()

但是你可能甚至不需要那个,因为String是IEnumerable,这意味着你可以在String上做一个foreach,而不是把它变成一个数组,就像我上面那样。

答案 1 :(得分:1)

试试这个,

// Read the lines (words) from a file and store them in a list
String[] myWordArrays = File.ReadAllLines("WordList.txt");

// Generate a random number and select the word at that index of the list myWordArrays
Random randomWord = new Random();            
int activeWord = randomWord.Next(0, myWordArrays.Length);
string randomlyChosenWord = myWordArrays[activeword];

// Query the user
Console.WriteLine("Are you Ready to play Hangman? yes/no: ");
string userSelection = Console.ReadLine();

// Initialize
if(userSelection == "yes")
{
    foreach(char letter in randomlyChosenWord)
    {
       Console.Write("_ ");
    }
}

答案 2 :(得分:0)

感谢大家,尤其是Marc B.这是修复它的解决方案。当然,它目前打印随机单词的次数与单词中的字符数一样多,但我会在一分钟内修复它。

如果你能看到更有效的方式,请随时告诉我!

private static void printWord()
    {
        String[] myWordArrays = File.ReadAllLines("WordList.txt");
        Random randomWord = new Random();
        //int lineCount = File.ReadLines("WordList.txt").Count();            
        int activeWord = randomWord.Next(0, myWordArrays.Length);
        string userSelection = "";
        string answerWord = myWordArrays[activeWord];

        Console.WriteLine("Are you Ready to play Hangman? yes/no: ");

        userSelection = Console.ReadLine();
            if(userSelection == "yes")
            {
                //This runs through the randomly chosen word and prints an underscore in place of each letter - it does work
                foreach(char letter in myWordArrays[activeWord])
                {
                    Console.Write(answerWord);
                    Console.Write("_ ");

                }

                //This prints to the console "Can you guess what this 'varyingLength' letter word is?" - it does work.

            }