修改其他if和|运营商

时间:2013-06-28 14:33:27

标签: 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) {
        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;
}
}

如何更好地重构代码?让它更具可读性。

9 个答案:

答案 0 :(得分:3)

使用枚举

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;
  }
}

答案 1 :(得分:1)

创建一个存储所有名称的数组。其中.month Number介于1和12之间,从数组中读取名称。

答案 2 :(得分:1)

不确定这是否正是您所要求的,但这是一个开始!如果您希望以其他方式进行评论,请发表评论。

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问题”。

答案 3 :(得分:1)

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;
}

答案 4 :(得分:1)

您应该使用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);
}

答案 5 :(得分:0)

String[] months = String[] {"Jan", "Feb", ... , "Dec"};

String monthName = months[monthNumber - 1];

答案 6 :(得分:0)

使用enum s。几个月及其 orderung /数字和天数不会改变,所以几个月是一个很好的例子,在何时以及如何使用它们。 如果需要numberOfDays,请让enum有一个int monthNumber。虽然你需要进入enum,但你可以获得很多,因为你可以重用你的代码,也可以省略你的if / switch语句。< / p>

答案 7 :(得分:0)

每月制作两个arrays和每月的日期数。两者的大小都是12。

月份数组按照从索引0到11的顺序放置月份名称,并按照从索引0到11的顺序排列天数。现在您可以按索引检索数据。

答案 8 :(得分:0)

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";
      }
}