すべての変数が正しく初期化され、この1つのメソッド内でのみ定義され、dayを整数として呼び出すと、読み込み可能な文字列に設定され、フォーマット。また、整数のdayLを使用する数学では、StringIndexOutOfBounds Exceptionが返されることがあります。これは、不等式に起因するエラーが原因であると理解しています。 ありがとうございました。スイッチケース内の文字列がNULLを返す
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);
}
'1!= '1''と' 2!=' 2''などなぜ 'switch(day)'にならないのですか?残りのコードのポイントは何ですか? –
これは星占いジェネレータプロジェクトのためのもので、私は複雑で、文字列を使用することで長い道のりをとることを選択しました。文字列の代わりにint-string-string変換を使用するか、そう思っています。btw – Kaden