简体   繁体   English

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

[英]Variable not initialized… but it was?

So the application I am working on at the moment requires 3 methods that return values to another class. 因此,我目前正在处理的应用程序需要3个将值返回到另一个类的方法。 The issue I'm having is with my second method that associates the users' input from the first method with the corresponding Named month. 我遇到的问题是第二种方法,该方法将第一种方法的用户输入与相应的“命名月份”相关联。 I continuously receive a "variable might not have been initialized" error from this set: 我不断收到来自此集合的“变量可能尚未初始化”错误:

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

Any help is greatly appreciated! 任何帮助是极大的赞赏!

You have to initialize the variable as : 您必须将变量初始化为:

String monthNameFin=null;

or 要么

String monthNameFin="";

else if you have always an assignment of value you can skip the initialization, for example: 否则,如果您始终有一个值分配,则可以跳过初始化,例如:

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

in this case there is no escape... to monthNumberFin will be in any case a value, while in your code it might happen that it never enters an if .. assuming it is for example montthumber=14 在这种情况下,无法逃脱……到monthNumberFin无论如何都将是一个值,而在您的代码中,它可能永远不会输入if ..假设它是例如montthumber = 14

There is always the possibility that none of your cases matches, if monthNumber is less than or equal to 0, or if it's 13 or greater. 如果monthNumber小于或等于0,或者大于等于13,则总是有可能没有匹配的案例。 This means that there is a possibility of the variable not being initialized. 这意味着存在变量未初始化的可能性。

Because numbers outside the range of 1-12 don't make sense, it's better to throw an IllegalArgumentException at the bottom if no case matches. 由于超出1-12范围的数字没有意义,因此如果没有大小写匹配,最好在底部抛出IllegalArgumentException

This example also includes changing all case assignment statements to return statements, so that there is a return or a throw for every case, even for the unmatched case. 此示例还包括将所有案例分配语句更改为return语句,以使每个案例(即使是不匹配的案例)都有returnthrow

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

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

The code that calls this method should catch this exception and handle it properly. 调用此方法的代码应捕获此异常并正确处理它。

As the other answers stated, you have to initialized your variable. 正如其他答案所述,您必须初始化变量。

But instead of all this if , I would use a DateFormatSymbols object. 但是, 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];
}

Simpler way to write this that handles all integer cases: 可以处理所有整数情况的更简单方法:

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

Also, 也,

(1) just initialize monthName at first, perhaps to (1)首先只初始化monthName,也许是

String month = monthName(monthNumber);

(2) you should probably begin method names with lowercase. (2)您应该以小写字母开头方法名称。

Should not enter the statement "IF" find an uninitialized variable. 不应在输入语句“ IF”中找到未初始化的变量。

Initializes monthNameFin. 初始化monthNameFin。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM