验证用户输入是否是字母

时间:2019-12-01 04:06:00

标签: c#

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

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

 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();



    }
}

2 个答案:

答案 0 :(得分:0)

您只需要添加一行即可。检查字母并打印完测试词后,将validLetterInput重置为false,以便它可以获取下一个字母。

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

答案 1 :(得分:0)

在第一次验证之后,您似乎没有将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