方法一:控制台输入月份

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 LIAO JIANYA
* 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 LIAO JIANYA
* 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 LIAO JIANYA
* 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. switch case语句

    五.switch case语句 1.格式 Switch(表达式) { case 表达式:语句块 break: … default break: } 2.例题 输入年份.月份.日期,判断是否是闰年,并且 ...

  2. 逆向知识第九讲,switch case语句在汇编中表达的方式

    一丶Switch Case语句在汇编中的第一种表达方式 (引导性跳转表) 第一种表达方式生成条件: case 个数偏少,那么汇编中将会生成引导性的跳转表,会做出 if else的情况(类似,但还是能分 ...

  3. 为什么说在使用多条件判断时switch case语句比if语句效率高?

    在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...

  4. java中的Switch case语句

    java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...

  5. switch… case 语句的用法

    switch… case 语句的用法   public class Test7 { public static void main(String[] args) { int i=5; switch(i ...

  6. if语句,if...else if语句和switch...case语句的区别和分析

    前段时间在工作中遇到了一个关于条件判断语句的问题,在if语句,if else if语句和switch case语句这三者之间分析,使用其中最有效率的一种方法. 所以就将这个问题作为自己第一篇博客的主要 ...

  7. Python | 基础系列 · Python为什么没有switch/case语句?

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

  8. 为什么switch...case语句比if...else执行效率高

    在C语言中,教科书告诉我们switch...case...语句比if...else if...else执行效率要高,但这到底是为什么呢?本文尝试从汇编的角度予以分析并揭晓其中的奥秘. 第一步,写一个d ...

  9. JavaScript基础知识(if、if else、else if、while、switch...case语句)

    13.语句 概念:就是分号(:) 代表一条语句的结束 习惯:一行只编写一条语句:一行编写多条语句(代码可读性较差) 语句块:可以包含多条语句     "{ }"将多条语句包裹 u ...

随机推荐

  1. JavaScript高级程序设计39.pdf

    第13章 事件 JavaScript与HTML之间的交互式通过事件来实现的. 事件流 事件流描述的是从页面中接收事件的顺序,IE和Netscape提出了完全相反的事件流概念,IE是事件冒泡流,Nets ...

  2. jQuery 参考手册 - 事件

    事件方法会触发匹配元素的事件,或将函数绑定到所有匹配元素的某个事件. bind()向匹配元素附加一个或更多事件处理器 $(selector).bind(event,function) $(select ...

  3. [SAM4N学习笔记]LED点灯程序

    一.准备工作:      将上一节搭建的工程模板复制一份,命名为"1.blink",这作为我们开发的第一个程序. 二.程序编写:      板子上只有一个可控制的LED,就是LED ...

  4. GCC内联汇编入门

    原文为GCC-Inline-Assembly-HOWTO,在google上可以找到原文,欢迎指出翻译错误. 中文版说明 由于译者水平有限,故译文出错之处,还请见谅.C语言的关键字不译,一些单词或词组( ...

  5. Linux I2C设备驱动编写(一)

    在Linux驱动中I2C系统中主要包含以下几个成员: I2C adapter 即I2C适配器 I2C driver 某个I2C设备的设备驱动,可以以driver理解. I2C client 某个I2C ...

  6. 《A First Course in Probability》-chape4-离散型随机变量-几种典型分布列

    超几何分布: 超几何分布基于这样一个模型,一个坛子中有N个球,其中m个白球,N-m个黑球,从中随机取n(不放回),令X表示取出来的白球数,那么: 我们称随机变量X满足参数为(n,m,M)的超几何分布. ...

  7. 用户home目录下的.gitconfig 和 库文件夹目录下的 .gitignore 示例

    .gitconfig文件: [user] name = hzh email = @qq.com [core] editor = vi quotepath = false [merge] tool = ...

  8. python-面向对象(一)——开篇基础

    面向对象编程(Object Oriented Programming,OOP,面向对象程序设计) 一.创建类和对象 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现, ...

  9. hdoj 1698 Just a Hook【线段树区间修改】

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. 一个免费的自动化跨平台测试JavaScript的工具——BrowserSwarm

    BrowserSwarm是一个免费工具,能够自动化跨平台测试JavaScript.