switch case中的字符串返回null

时间:2016-10-04 03:08:00

标签: java methods

所有变量都已正确初始化,并且仅在此一个方法中定义,将day调用为整数,然后设置为能够读取的字符串,然后拆分为字符以创建单词格式。 此外,使用dayL(整数)的数学有时会返回StringIndexOutOfBounds Exception,我理解这是由于我使用.length()创建的不等式引起的错误; 谢谢你的帮助。

    public static void bday()
{
    s_day = Integer.toString(day);
    dayL = s_day.length();

    switch (dayL)
    {
    case 1:
        if(s_day.charAt(0) == 1)
        {
            word_day = "first";
        }
        else if(s_day.charAt(0) == 2)
        {
            word_day = "second";
        }
        else if(s_day.charAt(0) == 3)
        {
            word_day = "third";
        }
        else if(s_day.charAt(0) == 4)
        {
            word_day = "fourth";
        }
        else if(s_day.charAt(0) == 5)
        {
            word_day = "fifth";
        }
        else if(s_day.charAt(0) == 6)
        {
            word_day = "sixth";
        }
        else if(s_day.charAt(0) == 7)
        {
            word_day = "seventh";
        }
        else if(s_day.charAt(0) == 8)
        {
            word_day = "eighth";
        }
        else if(s_day.charAt(0) == 9)
        {
            word_day = "ninth";
        }
        break;

    case 2:
        //teens

        if(s_day.charAt(0) == 1 && s_day.charAt(1) == 0)
        {
            word_day = "tenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 1)
        {
            word_day = "eleventh";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 2)
        {
            word_day = "twelfth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 3)
        {
            word_day = "thirteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 4)
        {
            word_day = "fourteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 5)
        {
            word_day = "fifteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 6)
        {
            word_day = "sixteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 7)
        {
            word_day = "seventeenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 8)
        {
            word_day = "eighteenth";
        }
        else if(s_day.charAt(0) == 1 && s_day.charAt(1) == 9)
        {
            word_day = "ninteenth";
        }

        //twenties

        if(s_day.charAt(0) == 2 && s_day.charAt(1) == 0)
        {
            word_day = "twentieth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 1)
        {
            word_day = "twenty-first";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 2)
        {
            word_day = "twenty-second";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 3)
        {
            word_day = "twenty-third";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 4)
        {
            word_day = "twenty-fourth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 5)
        {
            word_day = "twenty-fifth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 6)
        {
            word_day = "twenty-sixth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 7)
        {
            word_day = "twenty-seventh";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 8)
        {
            word_day = "twenty-eighth";
        }
        else if(s_day.charAt(0) == 2 && s_day.charAt(1) == 9)
        {
            word_day = "twenty-ninth";
        }

        //thirties

        if(s_day.charAt(0) == 3 && s_day.charAt(1) == 0)
        {
            word_day = "thirtieth";
        }
        else if(s_day.charAt(0) == 3 && s_day.charAt(1) == 1)
        {
            word_day = "thirty-first";
        }
        break;
    }
    System.out.println("Your birthday is: " + s_month + " "+ word_day);
}

3 个答案:

答案 0 :(得分:3)

每次比较一个角色时,你做错了。

s_day.charAt(0) == 1

应该是

s_day.charAt(0) == '1'

但即使这样也比它需要的更复杂。您在变量int中将{0}作为day,对吗?那么为什么不在此基础上发表if声明:

if (day == 1) {
  word_day = "first";
}else if (day == 2)
//and so on

答案 1 :(得分:1)

你应该使用

s_day.charAt(0)=='1'

我建议你可以写这样的方法:

String [] days={"first","second","third",.......,"thirty-first"};
word_day=days[day%31];

答案 2 :(得分:0)

请重构此代码,如@fei_hsueh建议或如下所示:

Map<Integer, String> days = new HashMap<>();
days.put(1, "first");
days.put(2, "second");
days.put(3, "third");
...

private static String bday(int day) {
    return days.get(day);
}
相关问题