gpt4 book ai didi

java - 编译器错误,变量nameOfMonth可能尚未初始化

转载 作者:行者123 更新时间:2023-12-02 11:16:08 24 4
gpt4 key购买 nike

我收到编译器错误,提示变量nameOfMonth可能尚未初始化。

我知道这可能是一个简单的解决方法,但我陷入了困境,似乎无法在Google上找到答案。

public class Switch
{
public static void main ( String [] args )
{
int month;
String monthString, nameOfMonth;
month=ConsoleInput.readInt("Enter Month in the form, e.g- January = 1, Febuary = 2, etc");
monthString = nameOfMonth;
System.out.println(monthString);
}

protected static String nameOfMonth(int month)
{
String monthString;
switch(month)
{
case 1: monthString = "January";
break;
case 2: monthString = "Febuary";
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;
}
}

最佳答案

在分配的右侧使用nameOfMonth之前,未对其进行初始化:

String monthString, nameOfMonth;
month=ConsoleInput.readInt(
"Enter Month in the form, e.g- January = 1, Febuary = 2, etc");
monthString = nameOfMonth; // Correct compiler error here.

我怀疑代码旨在调用相同的命名方法。
monthString = nameOfMonth(month);

并且不需要 nameOfMonth变量。

关于java - 编译器错误,变量nameOfMonth可能尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16645109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com