如果我想说的是,检查的int
是某两个数字之间,这将是更好地实现它没有例外,像这样:检查错误
int a; //example in C++, but translate into your language of choice...
cin >> a;
if(a < 0 || a > 5)
cout << "You must enter in a number between 0 and 5 inclusive!" << endl;
或者在尝试异常 - catch块?
try
{
int a;
cin >> a;
if(a < 0 || a > 5)
throw "You must enter in a number between 0 and 5 inclusive!" << endl;
}
catch(string msg)
{
cout << msg << endl;
}
我至少会改变它为`if((unsigned)a> 5)`方式。 ;-) – 2010-12-01 05:14:54