方法一:控制台输入月份

package com.liaojianya.chapter1;import java.util.Scanner;/*** This program demonstrates thw way of implements * display the number of days according to 12 months.* @author Hugh* 2016年7月19日*/
public class MonthAndDays
{public static void main(String[] args){System.out.println("please enter a number : 1~12");@SuppressWarnings("resource")Scanner scan = new Scanner(System.in);int month = scan.nextInt();switch (month){case 1:System.out.println("January has 31 days");break;case 2:System.out.println("February has 28 days");break;case 3:System.out.println("March has 31 days");break;case 4:System.out.println("April has 30 days");break;case 5:System.out.println("May has 31 days");break;case 6:System.out.println("June has 30 days");break;case 7:System.out.println("July has 31 days");break;case 8:System.out.println("August has 31 days");break;case 9:System.out.println("September has 30 days");break;case 10:System.out.println("Octor has 31 days");break;case 11:System.out.println("November has 30 days");break;case 12:System.out.println("December has 31 days");break;default: System.out.println("Error month information");}}}

  运行结果:

please enter a number : 1~12
4
April has 30 days

  方法二:随机产生1-12之间的某个整数:

package com.liaojianya.chapter1;
/*** This program demonstrates thw way of implements * display the number of days according to 12 months.* @author Hugh* 2016年7月19日*/
public class MonthAndDays
{public static void main(String[] args){int month = (int)(Math.random() * 12);System.out.println("随机产生一个月份并显示月份: " + month);switch (month){case 1:System.out.println("January has 31 days");break;case 2:System.out.println("February has 28 days");break;case 3:System.out.println("March has 31 days");break;case 4:System.out.println("April has 30 days");break;case 5:System.out.println("May has 31 days");break;case 6:System.out.println("June has 30 days");break;case 7:System.out.println("July has 31 days");break;case 8:System.out.println("August has 31 days");break;case 9:System.out.println("September has 30 days");break;case 10:System.out.println("Octor has 31 days");break;case 11:System.out.println("November has 30 days");break;case 12:System.out.println("December has 31 days");break;
//          default:
//              System.out.println("Error month information");}}}

  运行结果:

随机产生一个月份并显示月份: 3
March has 31 days

  分析:随机产生月份时,default可以省略。

显示12个月各自全部的天数:

package com.liaojianya.chapter1;
/*** This program demonstrates the way of using array to storage days of each month.* @author Hugh* 2016年7月19日*/
public class ArrayDemo
{public static void main(String[] args){int[] month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};for(int i = 0; i < month.length; i++){System.out.println("第" + (i+1) + "月有" + month[i] + "天!");            }}}

  运行结果:

第1月有31天!
第2月有28天!
第3月有31天!
第4月有30天!
第5月有31天!
第6月有30天!
第7月有31天!
第8月有31天!
第9月有30天!
第10月有31天!
第11月有30天!
第12月有31天!

  

使用switch case语句来显示月份的对应天数相关推荐

  1. 如何解决大量的if语句或switch case语句?

    洪流学堂,让你快人几步. 本篇内容来自洪流读书会解读书籍<代码大全2>. 很多面试官喜欢问这样的问题,如何解决大量的if语句或switch case语句?如果你仅仅在优化的层面回答,可能不 ...

  2. C语言case次数有限制吗,用switch...case语句统计数字、空格和其他字符出现的次数...

    //用switch...case语句统计数字.空格和其他字符出现的次数 //转自K&R #include int main(void) { int c, i, nwhite, nother, ...

  3. 在C++中对字符串std::string使用switch/case语句

    如果你使用C语音的string,也就是char *,是可以放在switch/case语句中的. 在C++中是不能对字符串string使用switch/case语句的,这里的string指的是std:: ...

  4. 在switch case 语句中能否使用continue 关键字?为什么?

    在switch case 语句中能否使用continue 关键字?为什么? #include <stdio.h>   int main()   {       int a;       p ...

  5. python中有没有switch_Python为什么没有switch/case语句?

    与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...

  6. java break在switch_java中switch case语句需要加入break的原因解析

    java中switch case语句需要加入break的原因解析 java 中使用switch case语句需要加入break 做了具体的实例分析,及编译源码,在源码中分析应该如何使用,大家可以参考下 ...

  7. python中case的用法_用 Python 实现简单的 switch/case 语句

    在Python中是没有Switch / Case语句的,很多人认为这种语句不够优雅灵活,在Python中用字典来处理多条件匹配问题字典会更简单高效,对于有一定经验的Python玩家不得不承认,的确如此 ...

  8. python中没有switch-case_Python为什么没有switch/case语句?

    与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...

  9. c语言 case语句用法,switch ... case语句的用法[组图]

    switch ... case语句的用法[组图] 08-13栏目:技术 TAG:switch case语句 switch case语句 当情况大于或等于4种的时候就用switch ...  case语 ...

最新文章

  1. pthread 立即停止线程_线程取消(pthread_cancel)
  2. Python中的文件操作和异常
  3. webp转换gif_右键转换文件格式
  4. 77 行代码实现 ABAP ALV 中的双击事件处理
  5. 【HDU - 3038】How Many Answers Are Wrong (带权并查集--权为区间和)
  6. 轻松做性能测试,月入3万的主流测试工具大揭秘
  7. java 情况浏览器缓存_JavaWeb禁止浏览器缓存当前Web页面的方法
  8. SSM整合配置文件总结
  9. 我学习的自定义ASP.NET分页控件
  10. word转PDF图片很糊or word自带公式编辑器公式自动编号转PDF括号没了
  11. 图像标注工具labelImg安装教程及使用方法
  12. 通过计算机和网络进行整个商务贸易活动,电子商务概论
  13. 机器学习--组合分类方法之随机森林算法原理和实现(RF)
  14. Python开发款短链生成器,来满足我的需求!
  15. 扫雷小游戏制作全过程
  16. 丁火生于未月命理分析_丁火生于未月
  17. HLS 开发学习(五) 稀疏矩阵向量乘法
  18. 排水沟槽开挖土方的计算方法(平行相似梯形组成的六面体体积分割计算方法)
  19. python字典统计单词个数_python字典统计单词个数
  20. RFSoC应用笔记 - RF数据转换器 -05- RFSoC关键配置之RF-ADC内部解析(三)

热门文章

  1. 不限速的远程链接软件ToDesk
  2. github 打不开_App更新不了? TF打不开? 试试这个
  3. HashMap底层理解(下)
  4. 计算机网络3-DNS域名解析系统
  5. phpcmsV9 公告内容(图片不显示问题)
  6. 语音合成技术与资源 - 资源篇
  7. Mint-UI 移动首页开发 - header导航、banner轮播图
  8. golang 排序_常用排序算法之冒泡排序
  9. c语言中格式化字符串系列函数包括,解析C语言中常用的格式化输入、输出函数...
  10. 某猪微店状元分销V2.0钻石版-全开源纯净安装版