岩纸剪刀游戏出错了

时间:2015-08-02 19:20:41

标签: c#

控制台应用程序摇滚纸剪刀游戏我很难解释它你只需要测试它,也可以看到每当我按y它想要清除控制台并开始游戏但这只是1/2每当我输入“摇滚”的问题时它将其翻译为“#”;它与纸和剪刀相同,它取出了第一个字母。

  enum Rock_Paper_Scissor
{
    rock,
    paper,
    scissor
}
class Computer
{
    Random rand = new Random();

    public Rock_Paper_Scissor Choice()
    {
        Rock_Paper_Scissor element = (Rock_Paper_Scissor)rand.Next(3);
        return element;
    }

}


class  Game_Logic
{

    static void Main()
    {
        Computer Comp = new Computer();
        bool PlayAgain = true;
        ConsoleKeyInfo input = Console.ReadKey();
        Rock_Paper_Scissor ComputerChoice = Comp.Choice();


           do
           {
               string PlayerChoice = Console.ReadLine();
               Console.Clear();

            while (PlayerChoice == ComputerChoice.ToString())
            {
                ComputerChoice = Comp.Choice();
            }

            Console.WriteLine("Player: "+ PlayerChoice);
            Console.WriteLine("Computer: " + ComputerChoice);

            if (PlayerChoice == "rock" && ComputerChoice.ToString() == "scissor")

                Console.WriteLine("You won!");

            else if (PlayerChoice == "scissor" && ComputerChoice.ToString() == "rock")

                Console.WriteLine("Computer won!");

            else if (PlayerChoice == "paper" && ComputerChoice.ToString() == "rock")

                Console.WriteLine("You won!");

            else if (PlayerChoice == "rock" && ComputerChoice.ToString() == "paper")

                Console.WriteLine("Computer won");

            else if (PlayerChoice == "scissor" && ComputerChoice.ToString() == "paper")

                Console.WriteLine("You won!");

            else if (PlayerChoice == "paper" && ComputerChoice.ToString() == "scissor")

                Console.WriteLine("Computer won!");
            else
            {
                Console.WriteLine("invalid value");
            }

            Console.WriteLine("\n"+"Play again? <y/n>");

            PlayAgain = input.KeyChar == 'y';

            } while (PlayAgain);


        }
    }

1 个答案:

答案 0 :(得分:1)

ConsoleKeyInfo input = Console.ReadKey();
Rock_Paper_Scissor ComputerChoice = Comp.Choice();
do
{
    string PlayerChoice = Console.ReadLine();

问题出在这个片段上。您的第一个字母将被触发,触发第一个Console.Readkey意味着您打算传递该值的Console.ReadLine()也将仅接收其余的信息。

Console.ReadKey()行无论如何都会在do循环之外没有任何好处,您也可以将其删除并执行

///
Console.WriteLine("\n"+"Play again? <y/n>");
} while (Console.ReadKey.KeyChar == 'y');

直接。解决这两个问题。