Java case语句没有初始化变量

时间:2018-06-01 00:03:16

标签: java switch-statement

如果我在JOptionPane中注释月份,我可以让我的代码工作。似乎交换机案例没有公开设置变量(如果这样做的话)。 这是我的代码。

    String[] sDateIn = jTextField1.getText().split("/");
    int mDate = Integer.parseInt(sDateIn[0]);
    int dDate = Integer.parseInt(sDateIn[1]);
    int yDate = Integer.parseInt(sDateIn[2]);
    String month;
    switch (mDate) {
        case 1:
            month = "Jan";
            break;
        case 2:
            month = "Feb";
            break;
        case 3:
            month = "Mar";
            break;
        case 4:
            month = "Apr";
            break;
        case 5:
            month = "May";
            break;
        case 6:
            month = "Jun";
            break;
        case 7:
            month = "Jul";
            break;
        case 8:
            month = "Aug";
            break;
        case 9:
            month = "Sep";
            break;
        case 10:
            month = "Oct";
            break;
        case 11:
            month = "Nov";
            break;
        default:
            month = "Dec";
            break;
    }        
    //display output
    JOptionPane.showMessageDialog(this, "The converted date is " + Month + " " + dDate + ", " + yDate);

1 个答案:

答案 0 :(得分:1)

好像您要追加Month而不是month

JOptionPane.showMessageDialog(this, "The converted date is " + month /*Month to month*/ + " " + dDate + ", " + yDate);