Java 变量“result”可能尚未初始化

Java 变量“result”可能尚未初始化,java,Java,公务舱月票{ 公共静态字符串month\u nameint month{ 字符串结果; 如果月份==1{ 结果=1月; }如果月份==2,则为else{ 结果=2月; }如果月份==3,则为else{ 结果=2月; }如果月份==4,则为else{ 结果=2月; }如果月份==5,则为else{ 结果=2月; }如果月份==6,则为else{ 结果=2月; }如果月份==7,则为else{ 结果=2月; }如果月份==8,则为else{ 结果=2月; }如果月份==9,则为else{ 结果=2月

公务舱月票{ 公共静态字符串month\u nameint month{ 字符串结果; 如果月份==1{ 结果=1月; }如果月份==2,则为else{ 结果=2月; }如果月份==3,则为else{ 结果=2月; }如果月份==4,则为else{ 结果=2月; }如果月份==5,则为else{ 结果=2月; }如果月份==6,则为else{ 结果=2月; }如果月份==7,则为else{ 结果=2月; }如果月份==8,则为else{ 结果=2月; }如果月份==9,则为else{ 结果=2月; }如果月份==10,则为else{ 结果=2月; }如果月份==11,则为else{ 结果=2月; }如果月份==12,则为else{ 结果=2月; } 返回结果; } 公共静态无效字符串[]args{ System.out.printlnMonth 1:+month\u name1; System.out.printlnMonth 2:+month\u name 2; System.out.printlnMonth 3:+month\u name3; System.out.printlnMonth 4:+month\u name 4; System.out.printlnMonth 5:+month\u name 5; System.out.printlnMonth 6:+month\u name 6; System.out.printlnMonth 7:+month\u name 7; System.out.printlnMonth 8:+month\u name 8; System.out.printlnMonth 9:+month\u name 9; System.out.printlnMonth 10:+month\u name 10; System.out.printlnMonth 11:+month\u name 11; System.out.printlnMonth 12:+month\u name 12; System.out.printlnMonth 43:+month\u name 43; }
} 局部变量应该在使用前初始化。它们没有默认值

初始化它

String result="";

局部变量应该在使用前初始化。它们没有默认值

初始化它

String result="";
由于month只是一个int,所以它可以接受if/else/if结构处理的值,而不是1-12

为了消除错误,您可以将其初始化为空字符串,但是,您可能需要考虑错误情况。

例如,如果传递的内容不在1-12范围内,该方法应该做什么?

因为month只是一个int,它可以接受if/else/if结构处理的值,而不是1-12

为了消除错误,您可以将其初始化为空字符串,但是,您可能需要考虑错误情况。

例如,如果传递的内容不在1-12范围内,该方法应该做什么?

变量结果尚未初始化。当输入介于1和12之间时,结果将仅包含一个值

您可以在声明变量时尝试此操作

String result = "";
或者,这样您就不需要添加另一行来将结果设置为错误

String result = "error";
变量结果尚未初始化。当输入介于1和12之间时,结果将仅包含一个值

您可以在声明变量时尝试此操作

String result = "";
或者,这样您就不需要添加另一行来将结果设置为错误

String result = "error";

如果你的可变月份可能是空的,或者是12个月左右,这可能会发生。您可以通过简单地删除if-else树中的最后一个if来解决此问题,例如:

公务舱月票{ 公共静态字符串month\u nameint month{ 字符串结果=; 如果月份==1{ 结果=1月; }如果月份==2,则为else{ 结果=2月; }如果月份==3,则为else{ 结果=2月; }如果月份==4,则为else{ 结果=2月; }如果月份==5,则为else{ 结果=2月; }如果月份==6,则为else{ 结果=2月; }如果月份==7,则为else{ 结果=2月; }如果月份==8,则为else{ 结果=2月; }如果月份==9,则为else{ 结果=2月; }如果月份==10,则为else{ 结果=2月; }如果月份==11,则为else{ 结果=2月; }否则{ 结果=2月; } 返回结果; } 公共静态无效字符串[]args{ System.out.printlnMonth 1:+month\u name1; System.out.printlnMonth 2:+month\u name 2; System.out.printlnMonth 3:+month\u name3; System.out.printlnMonth 4:+month\u name 4; System.out.printlnMonth 5:+month\u name 5; System.out.printlnMonth 6:+month\u name 6; System.out.printlnMonth 7:+month\u name 7; System.out.printlnMonth 8:+month\u name 8; System.out.printlnMonth 9:+month\u name 9; System.out.printlnMonth 10:+month\u name 10; System.out.printlnMonth 11:+month\u name 11; System.out.printlnMonth 12:+month\u name 12; System.out.printlnMonth 43:+month\u name 43; }
} 如果你的可变月份可能是空的,或者是12个月左右,这可能会发生。您可以通过简单地删除if-else树中的最后一个if来解决此问题,例如:

公务舱月票{ 公共静态字符串month\u nameint month{ 字符串结果=; 如果月份==1{ 结果=1月; }如果月份==2,则为else{ 结果=2月; }如果月份==3,则为else{ 结果=2月; }如果月份==4,则为else{ 结果=2月; }如果月份==5,则为else{ 结果=2月; }如果月份==6,则为else{ 结果=2月; }如果月份==7,则为else{ 结果=2月; }如果月份==8,则为else{ 结果=2月; }如果月份==9,则为else{ 结果=2月; }如果月份==10,则为else{ 结果=2月; }如果月份==11,则为else{ 结果=2月; }否则{ 结果=2月; } 返回结果; } 公共静态无效字符串[]args{ System.out.printlnMonth 1:+month\u name1; System.out.printlnMonth 2:+month\u name 2; System.out.printlnMonth 3:+month\u name3; System.out.printlnMonth 4:+month\u name 4; System.out.printlnMonth 5:+month\u name 5; System.out.printlnMonth 6:+month\u name 6; System.out.printlnMonth 7:+month\u name 7; System.out.printlnMonth 8:+month\u name 8; System.out.printlnMonth 9:+month\u name 9; System.out.printlnMonth 10:+month\u name 10; System.out.printlnMonth 11:+month\u name 11; System.out.printlnMonth 12:+month\u name 12; System.out.printlnMonth 43:+month\u name 43; } }