第9周-本月有几天?两种方法

来源:互联网 发布:sql语句建立table 编辑:程序博客网 时间:2024/05/22 08:02
方法一:if-else型#include <iostream>using namespace std;int main(){ int year,month;     cout<<"请输入年、月"<<endl;cin>>year>>month;if ( month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 )//应注意“==”才是“等于”的意思cout<<"本月有31天。";    else if ( month==4 || month==6 || month==9 || month==11 )    cout<<"本月有30天。";    else if ((year%4==0  &&  year%100!=0)||(year%400==0))//翻译一下:(能被4整除的非整百年份)或(能被400整除的年份)    cout<<"本月有29天。";    else    cout<<"本月有28天。";return 0;}方法二:switch型#include<iostream>using namespace std; int main()  {  int  year, month;  cout << "请输入年、月: "<<endl;cin >> year >> month;switch(month){case 1:case 3:case 5:case 7:case 8:case 10:case 12: cout<<"本月有31天。"; break;case 4:case 6:case 9:case 11:cout<<"本月有30天。";break;case 2:if((year%4==0 && year%100!=0) ||(year%400==0))cout<<"本月有29天。";elsecout<<"本月有28天。";}return 0;  }  

 

运行结果:

木有输出,表示不尴尬~

心得体会:

       其实这 两种方法大同小异。

       注意“==”才是“等于”的意思。