- /*
- * Copyright (c) 2014, 烟台大学计算机学院
- * All rights reserved.
- * 文件名称:test.cpp
- * 作 者:刘畅
- * 完成日期:2014年 10 月 16 日
- * 版 本 号:v1.0
- *
- * 问题描述:输入年份和月份,输出本月有多少天;
- * 输入描述: 输入年份year,月份month;
- * 程序输出:输出本月?天。
- */
- (1)if~
#include <iostream> using namespace std; int main() { int year, month, x; cout << "请输入年份和月份:"; cin >> year >> month; if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) x = 1; else x = 2; } else x = 1; } else x = 2; if (month == 2) if (x == 1) cout << "本月有29天"; else cout << "本月有28天"; else 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 cout << "您的输入有错误,请重新输入\n"; return 0; }
- (2)switch语句
#include <iostream> using namespace std; int main() { int year,month; cout<<"请输入年份和月份:"; cin>>year>>month; if (month!=2) switch (month) { case 1: case 3: case 7: case 8: case 10: case 12: cout<<"本月有31天"; break; case 4: case 6: case 9: case 11: cout<<"本月有30天"; break; } else { if ((year%4==0&&year%100!=0)||year%400==0) cout<<"本月有29天"; else cout<<"本月有28天"; } return 0; }
- 运行结果:
- 知识点总结:
- 对if~else~嵌套和switch语句开始有种得心应手的感觉,也开始试着多多运用& ,||, ! 这三种逻辑语句,还有"=="这个也在一次加深了理解。
- 学习心得:
- haha~ 贺老这周布置的任务已经全部完成,对if和switch有了初步的理解,虽然有点累,一下午都在写博客,但还是很开心啊
- 学习编程最好的方式果然还是多多练习,加油!! 我要更加努力 才能带领我们团队更早地腾飞
第8周项目6-本月有几天(if~else~嵌套语句和switch语句)
最新推荐文章于 2021-12-06 23:15:25 发布