Word hangingman C# 仅使用循环

Word Hangingman C# Using Only Loops

提问人:diogenes31 提问时间:4/26/2023 最后编辑:vimuthdiogenes31 更新时间:4/30/2023 访问量:43

问:

我正在尝试制作一个刽子手猜词游戏。我已经走到了这一步。现在唯一的问题是,当用户输入错误的字母时,我想插入“单词中没有{0}”的语句 playerGuess。此外,还有一份声明说“是的!单词中有{0}“,当用户输入正确的字母时,playerGuess。我还附上了一张我希望程序如何执行的图片。TYVM!

enter image description here

static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Hangman!!!!!!!!!!");
            string[] listwords = new string[10] 
            {"sheep", "goat","computer","america","watermelon","icecream",
            "jasmine","pineapple","orange","mango"};

            Random randGen = new Random();
            //Get random number
            var idx = randGen.Next(0, 9);
            //Us the random number as index 
            string mysteryWord = listwords[idx];
            //Create an array to store user's guesses
            char[] guess = new char[mysteryWord.Length]; 
            //Make chosen word as asterisks
            for (int p = 0; p < mysteryWord.Length; p++)
            {
                guess[p] = '*';
                Console.Write(guess[p]);
            }

            Console.WriteLine();

            //Loop iterates to check user input char against the chosen word
            while (true)
            {
                //Ask user for input
                Console.WriteLine("Please enter your guess: ");
                char playerGuess = char.Parse(Console.ReadLine());
                for (int j = 0; j < mysteryWord.Length; ++j)
                {
                    //What will happen when input is matched
                    if (playerGuess == mysteryWord[j])
                    {
                        guess[j] = playerGuess;
                    }
                }
                
                for(int j=0;j<mysteryWord.Length; ++j)
                {
                    //What will happen when the input is not matched
                    if (playerGuess != mysteryWord[j])
                    {
                        Console.WriteLine("There is no {0} in the word", playerGuess);
                        break;
                    }
                }

                Console.Write(guess);
                Console.WriteLine();

                //Print the word and end the game
                string guessString = new string(guess);
                if (guessString == mysteryWord)
                {
                    Console.Write("You win! The word is {0}", guessString);
                    Console.WriteLine();
                    return;
                }
                         
            }
        }
                    /*//What will happen when the input is not matched
                      //Putting this if statement within the for block cause the program to 
                      //not do anything else after one match guess. And if the playerGuess has more     than one match in the word, the loop ignore it. I think it has something to do with the break statement.

                    if (playerGuess != mysteryWord[j])
                    {
                        Console.WriteLine("There is no {0} in the word", playerGuess);
                        break;
                    }
                    */

为另一个 if 条件添加另一个 for 循环

                for (int j = 0; j < mysteryWord.Length; ++j)
                {
                    //What will happen when the input is not matched
                    if (playerGuess != mysteryWord[j])
                    {
                        Console.WriteLine("There is no {0} in the word", playerGuess);
                        break;
                    }
                }
/*This code got me the closest to what I want. However, the statement still display even when a match is found. I assumed that because it inside the loop, and there will always be playerGuess != mysteryWord[j]*/
C# 循环 while 套 for 循环

评论


答:

1赞 Agasi Mkhitaryan 4/30/2023 #1

即使在迭代字符串时,是否存在循环限制?因为要检查字符串是否包含字母,您可以简单地使用如下函数:Contains()

if (mysteryWord.Contains(playerGuess))
{
    Console.WriteLine("Yes! There is {0} in the word", playerGuess);
}
else
{
    Console.WriteLine("There is no {0} in the word", playerGuess);
}