我正在尝试创建一个程序,提示用户输入一个单词,它会返回单词的长度。但是,我遇到了“String word;”的问题,因为我不断收到局部变量可能尚未初始化的错误。
import javax.swing.JOptionPane;
public class FunctionsTest {
public static void main(String[] args) {
String word;
JOptionPane.showMessageDialog(null, "The length of the word is" + findLength (word));
}//End of Main Method
public static int findLength(String word) {
String str = new String(word);
return str.length();
}//End of findLength Method
}
最佳答案
“word”没有值,它是 null,您需要给“word”一些值:
String word = "abc";
关于java - 变量未在函数中初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37451149/