版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cd1202/article/details/51106354
随机给出一个0-99之间的数字,让你猜是什么数字。你可以随便猜一个数字,游戏会提示你太大或太小,从而缩小结果范围,经过几次猜测和提示后,最终猜出答案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rd = new Random(); //生成随机数n
int n = rd.Next(99);
int p = -1; //p为玩家猜的数
int t = 0; //t为猜的次数
while (p != n && t < 10)
{
string s = Console.ReadLine();
p = int.Parse(s);
if (p > n)
Console.WriteLine("大了");
else if (p < n)
Console.WriteLine("小了");
t++;
}
if (t< 10)
Console.WriteLine("猜对啦!这个数就是" + n);
else
Console.WriteLine("很遗憾猜了十次均不正确,这个数字是" + n);
Console.ReadKey();
}
}
}
运行结果: