输入年月,输出月份有几天(分别用了if——else和switch)
2024-08-26 11:04:20
首先是switch做的
class Program { static void Main(string[] args) {/* 题目要求:请用户输入年份,输入月份,输出该月的天数。 思路:一年中月份的情况有三种。 第一种:1,3,5,7,8,10,12月是31天。 第二种:4,6,9,11月是30天。 第三种:要判断年份是否是闰年,闰年2月29天。 平年2月28天。判断公式:year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)。 */ Console.WriteLine("请输入任意年份"); int year = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入任意月份"); int month = Convert.ToInt32(Console.ReadLine()); switch (month)//括号内跟的是要判断的表达式,结果必须是一个”值“(也就是一个确定的数)。 { ://当switch中的表达式有多种相同结果时,可以把case依次列出,最后break。 : : : : : : Console.WriteLine("您输入的{0}年中的{1}月份有31天。", year, month); break; ://case语句中嵌套了if——else的语句,来判断是否是闰年。 == || (year % == && year % != ))//判断闰年的表达式 { Console.WriteLine("您输入的{0}年中的2月份有29天。", year); } else//不是闰年执行这里。 { Console.WriteLine("您输入的{0}年中的2月份有28天。", year); } break; default ://case中剩余的情况就是月份是30天的。 Console.WriteLine("您输入的{0}年中的{1}月份有30天。", year); break; } Console.ReadLine(); } } }
用if——else做的
class Program { static void Main(string[] args) { Console.WriteLine("请输入一个年份。"); int year = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入年份中的任意一个月份。"); int month = Convert.ToInt32(Console.ReadLine()); || month == || month == || month == || month == || month == || month == ) { Console.WriteLine("{0}年的{1}月份有31天。",year,month); } || month == || month == || month == ) { Console.WriteLine("{0}年的{1}月份有30天。", year, month); } else { == || (year % == && year % != )) { Console.WriteLine("{0}年的2月份有29天", year); } else { Console.WriteLine("{0}年的2月份有28天",year); } } Console.ReadLine(); } } }
最新文章
- 解决 The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
- Spring 通过工厂配置Bean
- ThinkPHP中where()方法的使用
- Stupid Tower Defense
- mysql 操作指令笔记
- selenium webdriver启动IE浏览器失败的解决办法
- 大整数算法[11] Karatsuba乘法
- HDU4662+无
- 世界最大射电望远镜(Arecibo)用于探測地外文明
- 【C++基础之十四】函参的缺省
- leetcode:linked_list_cycle_II
- 使用element ui 日期选择器获取值后的格式问题
- Android的ProgressBar进度条-android学习之旅(三十一)
- 出错:Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'cn.mgy.mapper.UserMapper.findById'.
- strcpy_s和strcpy()
- JavaScript中的注释问题详解? 部分3
- 百度软件开发实习生c++方向面经(一面)
- nginx禁止未绑定域名访问返回444
- 00009 - cat、tail、head、tee、wc、sort文件操作和过滤
- MEAN 27