所有变量都已正确初始化,并且仅在该方法中定义,将 day 作为整数调用,然后设置为能够读取的字符串,然后拆分为字符以创建单词格式。 另外,使用 dayL(一个整数)进行数学运算有时会返回 StringIndexOutOfBounds 异常,我理解这是由于我使用 .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);
}
最佳答案
每次你比较一个角色,你就做错了。
s_day.charAt(0) == 1
应该是
s_day.charAt(0) == '1'
但即便如此,也比需要的复杂得多。您将日期作为变量 day
中的 int
存储,对吧?那么为什么不基于此创建一个 if
语句:
if (day == 1) {
word_day = "first";
}else if (day == 2)
//and so on
关于java - switch case 内的字符串返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39843551/