你的位置:首页 > ASP.net教程

[ASP.net教程]C#处理猜拳问题(非窗体)


//猜拳,5局3胜,要求使用公用变量。

namespace 结构体复习_公用变量

{

    class Program

    {public int rz=0;

        public int dz = 0;

 

        public string bj(int a , int b)

        {

            int d = a - b;

            if (d==-2||d==1)

            {

                return "人";

            }

            else if (d==2||d==-1)

            {

                return "电脑";

            }

            else

            {

                return "和";

            }

        }

        static void Main(string[] args)

        {Random sj = new Random();

            Program zdy=new Program();

            while (zdy.rz < 3 && zdy.dz < 3)

            {

                Console.WriteLine("请输入剪刀石头布的数字编码(1为剪刀,2为石头,3为布):");

                int a = int.Parse(Console.ReadLine());

                int b = sj.Next(1,4);

                string jg = zdy.bj(a, b);

                switch (jg)

                {

                    case "人":

                        zdy.rz++;

                        Console.WriteLine("本局你获胜");

                        break;

                    case "电脑":

                        zdy.dz++;

                        Console.WriteLine("本局电脑获胜");

                        break;

                    case "和":

                        Console.WriteLine("本局平局");

                        break;

                }

               

            }

            if (zdy.rz > zdy.dz)

            {

                Console.WriteLine("你赢了");

            }

            else

            {

                Console.WriteLine("你输了");

 

            }

            Console.ReadLine();

       }

    }

}