课堂练习 猜数字
程序员文章站
2022-07-08 08:59:15
...
题目
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 猜数字
{
class Program
{
static void Main(string[] args)
{
Random ram = new Random();
int guess = ram.Next(0, 101);
int i = 1;
int num = 0;
Console.WriteLine("猜测一个0到100之间的整数");
do
{
try
{
Console.Write("第{0}次猜,请输入一个整型数字:", i);
num = int .Parse(Console.ReadLine());
i++;
if (num == guess)
{
Console.WriteLine("恭喜你猜对了,这个数是{0}",num );
}
else if (num > guess)
{
Console.WriteLine("太大");
}
else
{
Console.WriteLine("太小");
}
}
catch (Exception ex)
{
Console.WriteLine("输入无效,请输入一个0到100之间的整数",ex.Message );
}
}while(num != guess);
Console.ReadLine();
}
}
}