簡體   English   中英

僅檢查字符串中的字母(字母)的 ReadKey

[英]Check ReadKey only for letters (alphabet) in string

我正在做游戲“劊子手”,但我有一個問題。 我正在使用 ReadKey 來找到玩家的猜測,但他可以使用偶數或“Enter”等按鈕。我想停止這個,但我不知道如何:/你能幫我嗎? 我想要控制,如果 ReadKey 的結果是字母表中的字母或不是(但它必須是“字符串”格式,因為我稍后會使用字符串)。 非常感謝你們,這是我的代碼:)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hangman
{
    class MainClass
    {
        /// <summary>
        /// Hra Oběšenec
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        { 


            Console.WriteLine("Vítejte ve hře Oběšenec!");
            //List slov, které bude hráč hádat
            string[] words = new string[5];
            words[0] = "car";
            words[1] = "school";
            words[2] = "apple";
            words[3] = "orange";
            words[4] = "xamarin";
            //Získávám délku slova (hádaného slova)

            for (int X = 0; X < words.Length; X++)
            {
                string guessWord = words[X];
                short lifes = 5;
                short guessedLetters = 0;
                char GuessingLetter;
                char[] lettersOfWord = new char[guessWord.Length];
                //
                for (int I = 0; I < guessWord.Length; I++)
                {
                    //Length of word is changing to *
                    lettersOfWord[I] = '*';
                }
                bool InGame = true;
                //When this while is true, the game is still going, when it will be false, the game will end
                while (InGame)
                {
                    //Ochrana proti špatnému inputu
                    try
                    {


                        Console.WriteLine(lettersOfWord);
                        Console.WriteLine("Stiskněte klávesu. Životy:" + lifes);
                        ConsoleKeyInfo result_guess = Console.ReadKey(true);
                        string result = result_guess.KeyChar.ToString();



                    if (result.Length == 0)
                    {
                        continue;
                    }
                    //Hráč hádá slovo
                    GuessingLetter = result[0];
                    lifes--;
                    for (int I = 0; I < lettersOfWord.Length; I++)
                    {
                        if (lettersOfWord[I] == '*')
                        {
                            //Pokud hráč uhádne písmenko ve slově 
                            if (guessWord[I] == GuessingLetter)
                            {
                                lettersOfWord[I] = GuessingLetter;
                                guessedLetters++;
                                lifes++;
                                Console.WriteLine("Správně!");
                            }                            
                        }
                    }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Nastala neočekávaná chyba!" + e);
                    }
                    //Pokud prohraje(promrhá všechny pokusy) hra skončí, avšak pokud slovo uhádne (a zůstane alespoň jeden život) vyhrál hru
                    if (lifes== 0 || guessedLetters == guessWord.Length)
                    {
                        //Konec hry 
                        InGame = false;
                        if (lifes == 0)
                        {
                            Console.WriteLine("Smůla prohrál jsi!");
                            Console.ReadKey();
                            break;
                        }
                        else
                        {
                            //Vítězství nad levlem
                            Console.WriteLine("Uhodnul jsi celé slovo! Gratuluji!!");
                        }
                    }
                }
            }
        }
    }
}

使用 IsLetter 方法檢查 keychar 是否為字母,如果不是,則繼續詢問字母。 像這樣的東西:

private char GetLetter()
{
    while (true)
    {
        Console.WriteLine("Please input a letter.");
        var character = Console.ReadKey().KeyChar;
        if (char.IsLetter(character))
            return character;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM