还有其他方法可以缩短这段特殊代码。 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 :(得分: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];
}
答案 1 :(得分:1)
public static final String var[] = {"January","February","March"....,"December"};
and then
months = var[month];//or month-1 if you index from 1