2017-09-21 2 views
-2

この特定のコードを短くする他の方法はありませんか? この問題を解決するために、javaに組み込まれている関数。 とにかく私は人々が私が尋ねたいものを理解できるように簡単なコードを書いています。スタックオーバーフローへこの短いコードを減らす方法はありますか?

enter code here 
    if(month==1){ 
     month1="January"; 
    } 
    if(month==2){ 
     month1="Febuary"; 
    } 
    if(month==3){ 
     month1="March"; 
    } 
    if(month==4){ 
     month1="April"; 
    } 
    if(month==5){ 
     month1="May"; 
    }enter code here 
    if(month==6){ 
     month1="June"; 
    } 
    if(month==7){ 
     month1="July"; 
    } 
    if(month==8){ 
     month1="August"; 
    } 
    if(month==9){ 
     month1="September"; 
    } 
    if(month==10){ 
     month1="October"; 
    } 
    if(month==11){ 
     month1="November"; 
    } 
    if(month==12){ 
     month1="December"; 
    } 
+0

ようこそ! [ツアー](ツアー)を見て回り、[ヘルプセンター](/ help)、特に[どのように良い質問をしますか?](/ help/how-to-質問)と[ここではどのような話題について聞くことができますか?](/ help/on-topic) ** - **あなたの質問は不明です。あなたのコードは動作しますか?もしそうなら、ここであなたの投稿を削除し、あなたの質問をhttps://codereview.stackexchange.com –

+2

https://stackoverflow.com/questions/1038570/how-can-i-convert-an-integer-to-現地通貨の月名Java – PhillipD

答えて

1
public static final String var[] = {"January","February","March"....,"December"}; 
and then 

months = var[month];//or month-1 if you index from 1 
+0

ありがとうございました。 –

2
private static final String[] MONTHS = { 
     "January", 
     "February", 
     "March", 
     "April", 
     "May", 
     "June", 
     "July", 
     "August", 
     "September", 
     "October", 
     "November", 
     "December", 
}; 

/** 
* Returns the name of the month with the stated number. 
* 
* @param monthNumber - The number is 1-based, i.e. 1 = January. 
* @return the name ofthe month as a string. 
*/ 
public String getMonthName(int monthNumber) { 
    return MONTHS[monthNumber - 1]; 
} 
+0

ありがとうございます。 –

+0

@yashbadia - こちらも知っています(https://stackoverflow.com/questions/1038570/how -can-i-convert-an-to-local-month-in-java)のテクニックを使用することができます。 – OldCurmudgeon

関連する問題