我尝试输入一个无效的日期,即2007年4月31日(4月是日历的第4个月,只有30天),我的代码执行了它应该执行的操作,但是为什么我的while循环会保持循环,甚至尽管我输入了有效日期?
class test
{
int nmonths;
int month;
int day;
int year;
public:
test()
{
int x;
do
{
cout << "Enter the date of most recent test (mm dd yyyy) (Ex. 01
09 2011):" <<endl;
cin >> month >> day >> year;
int days;
if ( month < 1 || month > 12 )
{
cout << "INVALID DATE!!!" << endl;
}
if ( month == 4 || month== 6 || month == 9 || month == 11)
{
days = 30;
if( day < 1 || day > 30)
{
cout << "INVALID DATE!!!" << endl;
x = 0;
}
}
if ( month == 2)
{
bool leapyear = (year% 4 == 0 && year % 100 != 0) || (year %
400 == 0);
if (leapyear == 0)
{
days = 28;
if (day < 1 || day > 28)
{
cout << "INVALID DATE!!!" << endl;
x = 0;
}
}
else
days = 29;
if ( day < 1 || day > 29 )
{
cout << "INVALID DATE!!!" << endl;
x = 0;
}
}
else
{
days = 31;
if ( day < 1 || day > 31)
{
cout << "INVALID DATE!!!" << endl;
x = 0;
}
}
}while(x == 0);
};
答案 0 :(得分:1)
您永远不会将score++;
$("#score").attr("value", score);
设置为非零值。
尝试在x
之前添加x = 1;
。