使用switch case语句来显示月份的对应天数
方法一:控制台输入月份
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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" ); } } } |
运行结果:
1 2 3 | please enter a number : 1 ~ 12 4 April has 30 days |
方法二:随机产生1-12之间的某个整数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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"); } } } |
运行结果:
1 2 | 随机产生一个月份并显示月份: 3 March has 31 days |
分析:随机产生月份时,default可以省略。
显示12个月各自全部的天数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 2 3 4 5 6 7 8 9 10 11 12 | 第 1 月有 31 天! 第 2 月有 28 天! 第 3 月有 31 天! 第 4 月有 30 天! 第 5 月有 31 天! 第 6 月有 30 天! 第 7 月有 31 天! 第 8 月有 31 天! 第 9 月有 30 天! 第 10 月有 31 天! 第 11 月有 30 天! 第 12 月有 31 天! |
烧不死的鸟就是凤凰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)