变量未初始化......但它是?

时间:2014-03-04 21:33:00

标签: java

因此,我目前正在处理的应用程序需要3个方法将值返回给另一个类。我遇到的问题是我的第二种方法将第一种方法的用户输入与相应的命名月相关联。我不断从这个集合中收到“可能尚未初始化的变量”错误:

public String MonthName(int monthNumber){
    /*This method assigns a Name based on the users input and
    **returns the proper name of the corresponding month 
    ** @para: monthNameFin is the final name of the month
    **        rest should be self-explanatory              */

    String monthNameFin;

    if (monthNumber == 1)
        monthNameFin = "January";
    if (monthNumber == 2)
        monthNameFin = "February";
    if (monthNumber == 3)
        monthNameFin = "March";
    if (monthNumber == 4)
        monthNameFin = "April";
    if (monthNumber == 5)
        monthNameFin = "May";
    if (monthNumber == 6)
        monthNameFin = "June";
    if (monthNumber == 7)
        monthNameFin = "July";
    if (monthNumber == 8)
        monthNameFin = "August";
    if (monthNumber == 9)
        monthNameFin = "September";
    if (monthNumber == 10)
        monthNameFin = "October";
    if (monthNumber == 11)
        monthNameFin = "November";
    if (monthNumber == 12)
        monthNameFin = "December";

    return monthNameFin;
}

非常感谢任何帮助!

5 个答案:

答案 0 :(得分:3)

您必须将变量初始化为:

String monthNameFin=null;

String monthNameFin="";

否则如果您总是赋值,则可以跳过初始化,例如:

String monthNameFin;
   if (monthNumber == 1){
        monthNameFin = "January";
   }else{ 
        monthNameFin = "January";
   }

在这种情况下没有逃避...对于monthNumberFin来说无论如何都是一个值,而在你的代码中可能会发生它永远不会输入if ...假设它是例如montthumber = 14

答案 1 :(得分:1)

如果monthNumber小于或等于0,或者如果它是13或更大,则总有可能没有任何案例匹配。这意味着变量可能未被初始化。

因为1-12范围之外的数字没有意义,所以如果没有案例匹配,最好在底部抛出IllegalArgumentException

此示例还包括将所有案例分配语句更改为return语句,以便每个案例都有returnthrow,即使对于无法匹配的案例也是如此。

  ...
  if (monthNumber == 12)
      return "December";

  // No case matched.
  throw new IllegalArgumentException("Bad month number: " + monthNumber);
}

调用此方法的代码应该捕获此异常并正确处理它。

答案 2 :(得分:1)

正如其他答案所述,您必须初始化您的变量。

但不是所有这些if,我会使用DateFormatSymbols对象。

public String MonthName(int monthNumber){
     if(monthNumber < 1 || monthNumber > 12)
           throw new IllegalArgumentException("Month must be in the range [1, 12]");
     DateFormatSymbols dfs = DateFormatSymbols.getInstance(Locale.UK);
     return dfs.getMonths()[monthNumber-1];
}

答案 3 :(得分:0)

编写处理所有整数情况的更简单的方法:

public String monthName(int monthNumber) {
    if (monthNumber < 1 || monthNumber > 12) return null;
    String[] months = new String[] {"January", "February", "March", "April", "May",
            "June", "July", "August", "September", "October", "November", "December"};
    return months[monthNumber];
}

另外,

(1)首先初始化monthName,或许是

String month = monthName(monthNumber);

(2)你应该用小写字母开始方法名称。

答案 4 :(得分:0)

不应输入语句“IF”找到未初始化的变量。

初始化monthNameFin。

试试这个:

public String MonthName(int monthNumber){
    /*This method assigns a Name based on the users input and
    **returns the proper name of the corresponding month 
    ** @para: monthNameFin is the final name of the month
    **        rest should be self-explanatory              */

    String monthNameFin = ""; /* <----------- */

    if (monthNumber == 1)
        monthNameFin = "January";
    if (monthNumber == 2)
        monthNameFin = "February";
    if (monthNumber == 3)
        monthNameFin = "March";
    if (monthNumber == 4)
        monthNameFin = "April";
    if (monthNumber == 5)
        monthNameFin = "May";
    if (monthNumber == 6)
        monthNameFin = "June";
    if (monthNumber == 7)
        monthNameFin = "July";
    if (monthNumber == 8)
        monthNameFin = "August";
    if (monthNumber == 9)
        monthNameFin = "September";
    if (monthNumber == 10)
        monthNameFin = "October";
    if (monthNumber == 11)
        monthNameFin = "November";
    if (monthNumber == 12)
        monthNameFin = "December";

    return monthNameFin;
}