使用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 ...
随机推荐
- leecode 排列的学习
前面写过3个排列.这里再写一次. 1.全部都不重复https://oj.leetcode.com/problems/permutations/ (使用交换法)只是本人对c++ stl不熟,不会把排列结 ...
- 南阳acm奇偶数分离
这道题的特殊要求是要先先限定了测试数据的组数,所以多加一条循环语句.下面是已通过的代码: #include<stdio.h> main() { int n,m,i,j; ...
- Android Studio 环境配置优化
一.插件 .ignore: 版本控制忽略文件高亮和补齐ADB Idea: ctrl + Shift + A 查找中添加常用卸载安装app的一些操作,无需命令行Android ButterKnife Z ...
- Git 版本控制工具(学习笔记)
GIT(分布式) 一.Git 初始版本控制工具 1. 安装Git Ubuntu系统下,打开shell界面,输入: sudo apt-get install git-core 之后回车输入密码,即可完 ...
- Android怎样改动app不在多任务列表中显示
在实际开发中,我们希望某些activity或者应用程序不在多任务列表中显示,即长按Home键或者多任务button键不显示近期执行的程序,我们能够在对应应用程序的AndroidManifest.xml ...
- DS18B20测温
项目需要实现分布式大规模测温,需要52个测温点,采样DS18B20进行设计. 30cm一个点,一共8个点串联.采用国标单芯单股纯铜硬线BV0.5做导线,测试一会儿正常,一会儿不正常.后面换线了,测试正 ...
- Velocity源码分析
velocity模板渲染的步骤: 1) 首先初始化启动Velocity引擎,可以通过Velocity.init()或者新建VelocityEngine类,并调用其中的init()方法: 2) 创建一个 ...
- Linux进程间通信——使用数据报套接字
前一篇文章, Linux进程间通信——使用流套接字介绍了一些有关socket(套接字)的一些基本内容,并讲解了流套接字的使用,这篇文章将会给大家讲讲,数据报套接字的使用. 一.简单回顾——什么是数据报 ...
- LabVIEW系列——合并错误(VI)的用法
Merge Errors.vi的功能:1.按顺序搜索错误输入1,2,3,以及错误数组输入中的错误,输出第一个错误. 2.如果没有错误,也就是错误状态都为F ...
- 基于keepalived对redis做高可用配置---转载
关于keepalived的详细介绍,请移步本人相关博客:http://wangfeng7399.blog.51cto.com/3518031/1405785 功能 ip地址 安装软件 主redis 1 ...