C++编程 用switch-case语句编程,输入月份名称(1~12月份),输出其英文名称和天数

2024-12-20 07:40:09
推荐回答(3个)
回答1:

//case后面改成数值,不是字符;
#include
using namespace std;
void main()
{int month;
cout<<"input a month between 1--12:";
cin>>month;
switch(month)
{
case 1:cout<<"January 31days\n";
break;
case 2:cout<<"February 28or29days\n";
break;
case 3:cout<<"March 31days\n";
break;
case 4:cout<<"April 30days\n";
break;
case 5:cout<<"May 31days\n";
break;
case 6:cout<<"June 30days\n";
break;
case 7:cout<<"July 31days\n";
break;
case 8:cout<<"August 31days\n";
break;
case 9:cout<<"September 30days\n";
break;
case 10:cout<<"October 31days\n";
break;
case 11:cout<<"November 30days\n";
break;
case 12:cout<<"December 31days\n";
break;
default:cout<<"the number is wrong.\n";
break;
}
}

回答2:

改成 case 1 这样子,不要引号

#include
using namespace std;
void main()
{int month;
cout<<"input a month between 1--12:";
cin>>month;
switch(month)
{
case 1:cout<<"January 31days\n";
break;
case 2:cout<<"February 28or29days\n";
break;
case 3:cout<<"March 31days\n";
break;
case 4:cout<<"April 30days\n";
break;
case 5:cout<<"May 31days\n";
break;
case 6:cout<<"June 30days\n";
break;
case 7 :cout<<"July 31days\n";
break;
case 8 :cout<<"August 31days\n";
break;
case 9 :cout<<"September 30days\n";
break;
case 10 :cout<<"October 31days\n";
break;
case 11 :cout<<"November 30days\n";
break;
case 12 :cout<<"December 31days\n";
break;
default:cout<<"the number is wrong.\n";
break;
}
}

回答3:

一楼正解