题目:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200308110852613.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0taSDM0,size_16,color_FFFFFF,t_70)
1.思路:
- 使用题目所给代码产生随机数。
- 判断猜测数与随机数的大小,并反馈给用户。
2.代码:
class Program
{
static void Main(string[] args)
{
int n;
int count = 1;
Console.WriteLine("猜测一个100以内的整数。");
Console.Write("第1次猜,请输入一个整数:");
Random rdm = new Random();
int num = rdm.Next(0, 101);
for (; ; )
{
count++;
try
{
string guess = Console.ReadLine();
n = int.Parse(guess);
if (n < num)
{
Console.WriteLine("太小");
Console.Write("第{0}次猜测,请输入一个整数:", count);
continue;
}
else if (n > num)
{
Console.WriteLine("太大");
Console.Write("第{0}次猜测,请输入一个整数:", count);
continue;
}
else
{
Console.WriteLine("恭喜你猜对了,这个数是{0}", num);
}
}
catch (Exception ex)
{
Console.WriteLine("输入数据无效,请重新输入。");
}
}
}}
3.补充:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200308111823834.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0taSDM0,size_16,color_FFFFFF,t_70)