static int ReadInt(int min,int max)
{
while(true)
{
try
{
int number = Convert.ToInt32(Console.ReadLine());
if(number<min || number>max)
{
Console.WriteLine("只能输入{0}到{1}的数字", min, max);
continue;
}
return number;
}
catch
{
Console.WriteLine("只能输入数字,请重新输入");
}
}
}一般都用try-catch判断 |