使用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天!

  

posted @   Andya_net  阅读(6011)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示