欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.NET Core异步编程

程序员文章站 2022-03-09 13:36:43
...

使用异步编程编写趣味猜拳游戏

 static void Main(string[] args)
        {
            string[] firstType = new string[] { "石头", "剪刀", "布" };
            Console.WriteLine("*************趣味猜拳游戏*************");
            Console.WriteLine("请输入你的出拳数字(1-石头,2-布,3-剪刀,4-退出)");
            string inputFist = Console.ReadLine();
            while (inputFist != "4")
            {
                Random random = new Random();
                int computerFist = (int)random.Next(1, 3);
                try
                {
                    int personFirst = Convert.ToInt32(inputFist);
                    if (personFirst > 0 && personFirst < 5)
                    {
                        if (personFirst < computerFist)
                        {
                            if (personFirst == 1 && computerFist == 3)
                            {
                                Console.WriteLine("你出了{0},电脑出了{1},结果你赢了", firstType[personFirst - 1], firstType[computerFist - 1]);
                            }
                            else
                            {
                                Console.WriteLine("你出了{0},电脑出了{1},结果电脑赢了", firstType[personFirst - 1], firstType[computerFist - 1]);
                            }
                        }
                        else if (personFirst == computerFist)
                        {
                            Console.WriteLine("你出了{0},电脑出了{1},结果你和电脑平局了", firstType[personFirst - 1], firstType[computerFist - 1]);
                        }
                        else
                        {
                            if (personFirst == 3 && computerFist == 1)
                            {
                                Console.WriteLine("你出了{0},电脑出了{1},结果电脑赢了", firstType[personFirst - 1], firstType[computerFist - 1]);
                            }
                            else
                            {
                                Console.WriteLine("你出了{0},电脑出了{1},结果你赢了", firstType[personFirst - 1], firstType[computerFist - 1]);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("你输入的数字有错误,请重新输入......");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine("------------------------------------------------------");
                Console.WriteLine("请输入出拳数字(1-石头,2-布,3-剪刀,4-退出):");
                inputFist = Console.ReadLine();
            }
        }
相关标签: c#