Java 修改else if和|运算符
我有这段代码,想知道是否存在使其更具可读性的方法 我的代码:Java 修改else if和|运算符,java,if-statement,refactoring,Java,If Statement,Refactoring,我有这段代码,想知道是否存在使其更具可读性的方法 我的代码: public String getMonthName() { if (1 == this.monthNumber) { monthName = "January"; } else if (2 == this.monthNumber) { monthName = "February"; } else if (3 == this.monthNumber) {
public String getMonthName()
{
if (1 == this.monthNumber) {
monthName = "January";
} else if (2 == this.monthNumber) {
monthName = "February";
} else if (3 == this.monthNumber) {
monthName = "March";
} else if (4 == this.monthNumber) {
monthName = "April";
} else if (5 == this.monthNumber) {
monthName = "May";
} else if (6 == this.monthNumber) {
monthName = "June";
} else if (7 == this.monthNumber) {
monthName = "July";
} else if (8 == this.monthNumber) {
monthName = "August";
} else if (9 == this.monthNumber) {
monthName = "September";
} else if (10 == this.monthNumber) {
monthName = "Oktober";
} else if (11 == this.monthNumber) {
monthName = "November";
} else if (12 == this.monthNumber) {
monthName = "December";
}
return monthName;
}
/**
* Gets the number of days in this month
* @return the number of days in this month in a non-leap year
*/
public int getNumberOfDays()
{
int numberOfDays;
if (monthNumber == 1 || monthNumber == 3 || monthNumber == 5 || monthNumber == 7 || monthNumber == 9 || monthNumber == 11) {
numberOfDays = 31;
} else if (monthNumber == 4 || monthNumber == 6 || monthNumber == 8 || monthNumber == 10 || monthNumber == 12) {
numberOfDays = 30;
} else {
numberOfDays = 28;
}
return numberOfDays;
}
}
如何更好地重构代码?使其更具可读性。制作一个存储所有名称的数组。如果this.monthNumber介于1和12之间,请从数组中读取名称。不确定这是否正是您所要求的,但这只是一个开始!如果您希望以其他方式进行评论
public string getMonthName(int nbr){
String[] months = {"January", "February", "Mars", "April", "May", "June", "July", "August", "September", "Oktober", "November", "December"}:
return months[nbr-1];
}
public int nbrOfDays(int nbr){
int[] nbrOfDays= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return nbrOfDays[nbr-1];
}
编辑:修复了“索引0问题”。使用
enum
s。月份及其顺序/数量和天数不会改变,因此月份是一个很好的例子,说明何时以及如何使用月份。
让枚举有一个intnumberOfDays
,如果需要monthNumber
。尽管您需要进入enum
s,但您将获得很多好处,因为您可以重用您的代码,也可以省略if/switch语句。使用enum
public String getMonthName(){
switch (this.monthNumber) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
return monthString;
}
public enum Month
{
JANUARY( "January", 31 ),
FEBRUARY( "February", 28 ),
...
DECEMBER( "December", 31 );
private String monthName;
private int numOfDays;
private Month ( String monthName, int numOfDays )
{
this.monthName = monthName;
this.numOfDays = numOfDays;
}
public String getDisplayName ( )
{
return monthName;
}
public int getNumOfDays ( )
{
return numOfDays;
}
}
您应该使用
Calendar
类,它是用来避免所有这些额外分支的。您也不必确定这是否是闰年,API将为您记录这一点
final String[] months = new String[]{ "January", "February", ... };
public String getMonthName() {
return months[Calendar.getInstance().get(Calendar.MONTH)];
}
public int getNumberOfDays() {
return Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
}
为月份和每月的日期数制作两个
数组。两个都是12号的
在month数组中,将月份名称按索引0到11的顺序排列,并将天数按索引0到11的顺序排列。现在您可以通过索引检索数据。为什么不直接使用日历
和SimpleDataFormat
?不要忘记数组索引是基于0的。世界末日是2012年,所以没有问题!!两件事:这不包括闰年,它不会编译,因为字符串周围缺少引号<代码>“一月”等…
我修正了引号。然而,我认为在这一点上,把闰年混为一谈会让人感到困惑,对莫思来说也是如此length@Hunter麦克米伦。提供的方法OP在一年内没有任何准备金。但是也可以采用这个枚举来识别闰年。我想我的主要观点是这个功能已经存在于标准SDK中了。为什么要再创造一次呢?@Hunter-McMillen。我同意您不应该重新设计日历(特别是有JodaTime),但是OP可以使用一个熟悉的示例来学习更好的编程技术。毕竟,教科书中充满了相同的内容。
public String getMonthName(){
switch (monthNumber) {
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return "Invalid month";
}
}
public String getMonthName(){
switch (monthNumber) {
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return "Invalid month";
}
}