【问题标题】:Validating that user input is a letter of the alphabet验证用户输入是字母表中的一个字母
【发布时间】:2020-03-26 02:13:00
【问题描述】:

我正在尝试制作一个刽子手游戏,它从单词的文本文件中随机选择一个单词。然后它以星号显示单词,并要求用户猜测单词的每个字母,如果他们猜对了它就会发现那个字母。他们一直玩,直到猜出单词中的所有字母。猜出单词后,它将显示数字错过并询问他们是否想再玩一次。

我遇到的问题是我正在尝试验证用户输入了单个字符并且它是字母表中的一个字母,但是当用户输入一个字母时它只会启动一个无限循环。不确定如何解决这个问题。

 static void Main(string[] args)
    {
        char[] guessed = new char[26];
        char guess = ' ';
        char playAgain= ' ';
        bool validLetterInput = false;
        bool validAnswer = false;


        int amountMissed = 0, index = 0;

        do
        {
            // initilization of word and testword so that we could generate a testword with the same length as original
            char[] word = RandomLine().Trim().ToCharArray();

            char[] testword = new string('*', word.Length).ToCharArray(); 
            char[] copy = word;

            Console.WriteLine(testword);
            Console.WriteLine("I have picked a random word on animals");
            Console.WriteLine("Your task is to guess the correct word");


            while (!testword.SequenceEqual(word))
            {
                while (!validLetterInput)
                {
                    try
                    {
                        Console.Write("Please enter a letter to guess: ");
                        guess = char.Parse(Console.ReadLine().ToLower());
                        //Checks if guess is letter or not
                        if (((guess >= 'A' && guess <= 'Z') || (guess >= 'a' && guess <= 'z')))
                        {
                            validLetterInput = true;
                        }
                        else
                        {
                            Console.WriteLine("Invalid Input");
                        }


                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);

                    }
                }

                bool right = false;
                for (int j = 0; j < copy.Length; j++)
                {
                    if (copy[j] == guess)
                    {
                        Console.WriteLine("Your guess is correct.");
                        testword[j] = guess;
                        guessed[index] = guess;
                        index++;
                        right = true;
                    }
                }
                if (right != true)
                {
                    Console.WriteLine("Your guess is incorrect.");
                    amountMissed++;
                }
                else
                {
                    right = false;
                }
                Console.WriteLine(testword);

            }
            Console.WriteLine($"The word is {string.Join("",testword)}. You missed {amountMissed} times.");
            while (!validAnswer)
            {
                try
                {
                    Console.WriteLine("Do you want to guess another word? Enter y or n: ");
                    playAgain = char.Parse(Console.ReadLine());
                    if(playAgain == 'y' || playAgain == 'Y' || playAgain == 'n' || playAgain == 'N')
                    {
                        validAnswer = true;
                    }
                    else
                    {
                        Console.WriteLine("Invalid input try again");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        } while (playAgain == 'y' || playAgain == 'Y');


        Console.WriteLine("Good-Bye and thanks for playing my Hangman game.");
    }
        public static string RandomLine()
    {

            // store text file in an array and return a random value
            string[] lines = File.ReadAllLines("E:\\Advanced1.csv");
            Random rand = new Random();
            return lines[rand.Next(lines.Length)].ToLower();



    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    您似乎没有在第一次验证后将 validLetterInput 重置为 false。

    
    while (!testword.SequenceEqual(word))
    {
        while (!validLetterInput) // <-- Need to reset this after first correct validation
        {
            try
            {
                Console.Write("Please enter a letter to guess: ");
                guess = char.Parse(Console.ReadLine().ToLower());
                //Checks if guess is letter or not
                if (((guess >= 'A' && guess <= 'Z') || (guess >= 'a' && guess <= 'z')))
                {
                    validLetterInput = true;
                }
                else
                {
                    Console.WriteLine("Invalid Input");
                }
    
    
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
    
            }
        }
        validLetterInput = false; // <--- ADD THIS HERE
    

    在第一个字母被接受为有效字符后,它永远不会为下一个字母重置。退出检查有效输入的 while 循环后,将 validLetterInput 重置为 false

    【讨论】:

      【解决方案2】:

      您只需添加一行。检查完字母并打印出testword后,将validLetterInput设置为false,这样就可以得到下一个字母了。

      if (right != true)
      {
          Console.WriteLine("Your guess is incorrect.");
          amountMissed++;
      }
      else
      {
          right = false;
      }
      Console.WriteLine(testword);
      validLetterInput = false;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-03
        • 1970-01-01
        • 2022-01-19
        • 2015-01-27
        • 1970-01-01
        相关资源
        最近更新 更多