2016-10-04 1 views
0

すべての変数が正しく初期化され、この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); 
} 
+0

'1!= '1''と' 2!=' 2''などなぜ 'switch(day)'にならないのですか?残りのコードのポイントは何ですか? –

+0

これは星占いジェネレータプロジェクトのためのもので、私は複雑で、文字列を使用することで長い道のりをとることを選択しました。文字列の代わりにint-string-string変換を使用するか、そう思っています。btw – Kaden

答えて

3

文字を比較するたびに間違っています。

s_day.charAt(0) == 1 

s_day.charAt(0) == '1' 

する必要がありますしかし、たとえそれはそれは必要以上の方法より複雑です。変数dayにはintという日があります。あなたは

s_day.charAt(0)=='1'

を使用する必要があります

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

私は引用符を見落とし、同様に提案していただきありがとうございます。 – Kaden

+0

@Kadenは喜んでお手伝いします。 upvoteして受け入れてください – nhouser9

1

そして、私はあなたがこのようなチエニル方法書くことができることをお勧めします:なぜちょうどそれに基づいてif声明を行わない

String [] days={"first","second","third",.......,"thirty-first"}; 
word_day=days[day%31]; 
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); 
} 
関連する問題