使用switch case语句来显示月份的对应天数
方法一:控制台输入月份
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语句来显示月份的对应天数的更多相关文章
- switch case语句
五.switch case语句 1.格式 Switch(表达式) { case 表达式:语句块 break: … default break: } 2.例题 输入年份.月份.日期,判断是否是闰年,并且 ...
- 逆向知识第九讲,switch case语句在汇编中表达的方式
一丶Switch Case语句在汇编中的第一种表达方式 (引导性跳转表) 第一种表达方式生成条件: case 个数偏少,那么汇编中将会生成引导性的跳转表,会做出 if else的情况(类似,但还是能分 ...
- 为什么说在使用多条件判断时switch case语句比if语句效率高?
在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...
- java中的Switch case语句
java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...
- switch… case 语句的用法
switch… case 语句的用法 public class Test7 { public static void main(String[] args) { int i=5; switch(i ...
- if语句,if...else if语句和switch...case语句的区别和分析
前段时间在工作中遇到了一个关于条件判断语句的问题,在if语句,if else if语句和switch case语句这三者之间分析,使用其中最有效率的一种方法. 所以就将这个问题作为自己第一篇博客的主要 ...
- Python | 基础系列 · Python为什么没有switch/case语句?
与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...
- 为什么switch...case语句比if...else执行效率高
在C语言中,教科书告诉我们switch...case...语句比if...else if...else执行效率要高,但这到底是为什么呢?本文尝试从汇编的角度予以分析并揭晓其中的奥秘. 第一步,写一个d ...
- JavaScript基础知识(if、if else、else if、while、switch...case语句)
13.语句 概念:就是分号(:) 代表一条语句的结束 习惯:一行只编写一条语句:一行编写多条语句(代码可读性较差) 语句块:可以包含多条语句 "{ }"将多条语句包裹 u ...
随机推荐
- scp 在不同机器上传文件
推荐个博客,挺好的.http://www.cnblogs.com/hyddd/archive/2009/09/19/1570224.html 在不同机器上传文件是一个很常见的需求,也有很多种方法.我只 ...
- Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6321 Solved: 2628[Su ...
- cuda(1) 最大并发量
Created on 2013-8-5URL : http://blog.sina.com.cn/s/blog_a502f1a30101mi6t.html@author: zhxfl转载请说明出处 c ...
- yum puppet 并整合控制台
上篇说了下在ubuntu12.04上安装puppet,安装的版本为puppet2.7.11版本,今天尝试了下在CentOS6.4系统上安装puppet 3.1.1版本,本文参考chenshake的文章 ...
- 《神经网络和深度学习》系列文章三:sigmoid神经元
出处: Michael Nielsen的<Neural Network and Deep Leraning>,点击末尾“阅读原文”即可查看英文原文. 本节译者:哈工大SCIR硕士生 徐伟 ...
- STM32 定时器用于外部脉冲计数
STM32 定时器用于外部脉冲计数 第一步,设置GPIO GPIO_InitTypeDef GPIO_InitStructure; /* PA0,PA12-> 左右脉冲输入 */GPIO_Ini ...
- MongoDB库设计原则及实践
MongoDB数据模型选择• CAP定理(Consistency ,Availability 和Partition Tolerance )– Consistency(一致性):数据一致更新,所有数据变 ...
- CSS3实现兼容性的渐变背景效果
一.CSS3实现兼容性渐变背景效果,兼容FF.chrome.IE 渐变效果,现在主流的浏览器FF.Chrome.Opera.IE8+都可以通过带有私有前缀的CSS3属性来轻松滴实现渐变效果,IE7及以 ...
- android考试题
一.选择题 1. Math.round(11.5)等于多少( ). Math.round(-11.5) 等于多少( C ). A.11 ,-11 B.11 ,-12 C.12 ,-1 ...
- Win8控制面板快捷键
Win8系统相比我们已经熟悉的Win7与XP系统有着一些特殊的变化,导致很多初次使用Win8系统的朋友感觉有点吃力,随着Win8系统即将于几天后正式发布,下面本文与大家分享大家比较关心的Win8控制面 ...