#include
using namespace std;
int main()
{
int month,day;
do
{
cout<< "请输入月日:";
cin >> month >> day;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day<1||day>31)
{
cout << "输入有误,请重新输入!" << endl;
continue;
}
break;
case 4:
case 6:
case 9:
case 11:
if (day<1||day>30)
{
cout << "输入有误,请重新输入" << endl;
continue;
}
break;
case 2:
if (day<1||day>29)
{
cout << "输入有误,请重新输入" << endl;
continue;
}
break;
default:
if (day<1||day>30)
{
cout << "输入有误,请重新输入" << endl;
continue;
}
break;
}
break;
}while (1);
cout << "输入正确" << endl;
};
我给你写明swtich进行区间判断的两种用法。
void main()
{
int month = 0 ,day = 0;
cout << "请输入月份" << endl;
cin >> month ;
switch(month)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
cout << "月份正确" << endl;
break;
defalt:
cout << "月份错误!" << endl;
break;
}
cout << "请输入日期" << endl;
cin >> day;
swtich(day/32) //day 为1~31时结果都为0
{
case 0:
{
if((month == 2 && day == 30 )||
(month == 2 && day == 31 )||
((month == 4 || mont == 6 || month == 9 || month == 11)
&&(day == 31))
{
count << "日期错误!" << endl; //考虑2月和30天的月份
break;
}
else
{
counr << "日期正确!" << endl;
break;
}
}
default:
count << "日期错误!" << endl;
break;
}
}
另外可以自己加上循环,免得每次只能运行一遍