当前位置:编程学习 > C#/ASP.NET >>

C#猜数字

            int i_random = new Random().Next() % 100;
            int i_guess = 0;
            int i_count = 0;
            Console.WriteLine("\t\t\t\t猜数字游戏\n\n");
            Console.WriteLine("\t\t这是一个猜字游戏,猜1~100之间的随机数\n\n");
            Console.WriteLine("请输入一个数");
           
            
            for (i_count = 0; i_count <= 10; i_count++)
            {
                
                i_guess = int.Parse(Console.ReadLine());
                if (i_guess < i_random)
                {
                    Console.WriteLine("太小了,大一些");

                }
                if (i_guess > i_random)
                {
                    Console.WriteLine("太大了,小一些");
                }
                if (i_guess == i_random && i_count <= 10)
                {
                    Console.WriteLine("恭喜您猜对了!");
                }
                else if (i_guess == i_random && i_count == 10)
                {
                    Console.WriteLine("千钧一发之刻,您猜对了!");
                }
                else if (i_count == 10 && i_count > 10)
                {
                    Console.WriteLine("对不起,您已猜了10次!"); 
                    break;
                }
                Console.WriteLine("请按游戏菜单,输入 Y 或 N: ");
                char i = char.Parse(Console.ReadLine());
                if (i == 'Y' || i == 'y')
                {
                    continue;
                }
                else
                {
                    break;
                }
                
            }
  如何实现输入1或0就开始游戏?
   Y或N继续游戏?
应该怎样改.....~~~·谢了! --------------------编程问答-------------------- 要在DOS下输入1或0就开始游戏?  我想,你是不是需要一个批处理指令?
  --------------------编程问答-------------------- 帮你稍微修改了一下,大体是不变的:

            int i_random = new Random().Next() % 100;
            int i_guess = 0;
            int i_count = 0;
            Console.Write("是否想开始游戏(0表示不,1表示开始):");
            int flag = Convert.ToInt32(Console.ReadLine());
            if (flag == 1)
            {                Console.WriteLine("\t\t\t\t猜数字游戏\n\n");
                Console.WriteLine("\t\t这是一个猜字游戏,猜1~100之间的随机数\n\n");
                for (i_count = 0; i_count <= 10; i_count++)
                {
                    Console.WriteLine("请输入一个数");
                    i_guess = int.Parse(Console.ReadLine());
                    if (i_guess < i_random)
                    {
                        Console.WriteLine("太小了,大一些");

                    }
                    if (i_guess > i_random)
                    {
                        Console.WriteLine("太大了,小一些");
                    }
                    if (i_guess == i_random && i_count <= 10)
                    {
                        Console.WriteLine("恭喜您猜对了!");
                    }
                    else if (i_guess == i_random && i_count == 10)
                    {
                        Console.WriteLine("千钧一发之刻,您猜对了!");
                    }
                    else if (i_count == 10 && i_count > 10)
                    {
                        Console.WriteLine("对不起,您已猜了10次!");
                        break;
                    }
                    Console.WriteLine("请按游戏菜单,输入 Y 或 N: ");
                    string i = Console.ReadLine();
                    if (i == "Y" || i == "y")                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                //结束
            }
--------------------编程问答--------------------

            int i_random = new Random().Next() % 100;
            int i_guess = 0;
            int i_count = 0;
            Console.Write("是否想开始游戏(0表示不,1表示开始):");
            int flag = Convert.ToInt32(Console.ReadLine());
            if (flag == 1)
            {
                Console.WriteLine("\t\t\t\t猜数字游戏\n\n");
                Console.WriteLine("\t\t这是一个猜字游戏,猜1~100之间的随机数\n\n");
                for (i_count = 0; i_count <= 10; i_count++)
                {
                    Console.WriteLine("请输入一个数");
                    i_guess = int.Parse(Console.ReadLine());
                    if (i_guess < i_random)
                    {
                        Console.WriteLine("太小了,大一些");

                    }
                    if (i_guess > i_random)
                    {
                        Console.WriteLine("太大了,小一些");
                    }
                    if (i_guess == i_random && i_count <= 10)
                    {
                        Console.WriteLine("恭喜您猜对了!");
                    }
                    else if (i_guess == i_random && i_count == 10)
                    {
                        Console.WriteLine("千钧一发之刻,您猜对了!");
                    }
                    else if (i_count == 10 && i_count > 10)
                    {
                        Console.WriteLine("对不起,您已猜了10次!");
                        break;
                    }
                    Console.WriteLine("请按游戏菜单,输入 Y 或 N: ");
                    string i = Console.ReadLine();
                    if (i == "Y" || i == "y")
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                //结束
             }
--------------------编程问答-------------------- Console.WriteLine("请按游戏菜单,输入 Y 或 N: ");
                    string i = Console.ReadLine();
                    if (i == "Y" || i == "y")
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
但是输入完第一轮的时候我想返回是否重新继续游戏怎么办咧?
~~~ --------------------编程问答-------------------- here:
int i_random = new Random().Next() % 100;
....
else
{}
goto here; --------------------编程问答-------------------- 我写了一个控制台你参考一下吧.

 static void Main(string[] args)
        {
            here:
            int x = new Random().Next(0, 100); //随机数的下限和上限.          
            Console.Write("是否想开始游戏(0表示不,1表示开始):");
            int start = Convert.ToInt32(Console.ReadLine());//把输入的数附值给变量start.

            if (start == 1)//当用户输入的数等于1时.
            {
                Console.WriteLine("\t\t\t\t猜数字游戏\n\n");
                Console.WriteLine("\t\t这是一个猜字游戏,猜1~100之间的随机数\n\n");

                for ( int num = 0; num <= 10; num++)//给用户十次机会猜数字. 
                {
                    Console.WriteLine("请输入一个数");
                    start = int.Parse(Console.ReadLine());
                    if (start < x)
                    {
                        Console.WriteLine("太小了,大一些");
                    }
                    if (start > x)
                    {
                        Console.WriteLine("太大了,小一些");
                    }
                    if (start == x && num <= 10)
                    {
                        Console.WriteLine("恭喜您猜对了!");
                        break;
                    }
                    else if (start == x && num == 10)
                    {
                        Console.WriteLine("千钧一发之刻,您猜对了!");
                        break;
                    }
                    else if (start != x && num == 10)
                    {
                        Console.WriteLine("对不起,您已猜了10次!");
                        break;
                    }                   
                }
                Console.WriteLine("你想重新开始游戏吗?请输入 Y(是) 或 N(否): ");/*之前你这句写在for的里面了 应该写在for循环的外面.*/
                string i = Console.ReadLine();
                if (i == "Y" || i == "y")
                {
                    goto here;//goto就是让程序回到到前面有 here: 标记的地方
                }
                else
                {
                    return;
                }
            }
            else
            {
                //如果最开始输入的不是0或1就退出程序.
                Console.WriteLine("退出程序!");
                Console.ReadLine();//按F5加上这句.ctrl+F5就不用了.
            }

        }
--------------------编程问答-------------------- 这个这样写的话好像继续玩游戏时,i_random的值还是上次游戏的值吧,




引用 3 楼 liuhz_jsmstc 的回复:
C# code
            int i_random = new Random().Next() % 100;
            int i_guess = 0;
            int i_count = 0;
            Console.Write("是否想开始游戏(0表示不,1表示开始):");
            int flag = Convert.ToInt32(Console.ReadLine());
            if (flag == 1)
            {
                Console.WriteLine("\t\t\t\t猜数字游戏\n\n");
                Console.WriteLine("\t\t这…
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,