根据相应的数字缩短打印月份名称的程序

Amb*_*er 3 java

该程序要求用户输入等于或等于1-12的任何数字.然后它将数字转换为将要打印的消息(复制程序以自己查看).有没有办法缩短代码?

import javax.swing.JOptionPane;

public class NumOfMonth {

public static void main(String[] args) {



    int num = Integer.parseInt (JOptionPane.showInputDialog ("Enter any number equal to or between 1-12 to display the month"));

    switch (num)
    {
    case 1:
        System.out.println ("The name of month number 1 is January");
        break;
    case 2:
        System.out.println ("The name of month number 2 is February");
        break;
    case 3:
        System.out.println ("The name of month number 3 is March");
        break;
    case 4:
        System.out.println ("The name of month number 4 is April");
        break;
    case 5:
        System.out.println ("The name of month number 5 is May");
        break;
    case 6:
        System.out.println ("The name of month number 6 is June");
        break;
    case 7:
        System.out.println ("The name of month number 7 is July");
        break;
    case 8:
        System.out.println ("The name of month number 8 is August");
        break;
    case 9:
        System.out.println ("The name of month number 9 is September");
        break;
    case 10:
        System.out.println ("The name of month number 10 is October");
        break;
    case 11:
        System.out.println ("The name of month number 11 is November");
        break;
    case 12:
        System.out.println ("The name of month number 12 is December");
        break;
        default:
            System.out.println ("You have entered an invalid number");
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

Mar*_*oun 11

是的,使用DateFormatSymbols:

return new DateFormatSymbols().getMonths()[num - 1];
Run Code Online (Sandbox Code Playgroud)

getMonths 返回月份字符串数组..

我强烈建议您在访问阵列之前检查边界.