0
我想在这里写一个程序,并在我的if-else语句中我有String messageToStudent
初始化如果taskCompleted == 'y' || taskCompleted == 'Y'
,并在我的else语句中初始化它的其他任何东西。但是,当我尝试编译它时说messageToStudent
可能尚未初始化。现在,我认为我通过代码中的一个messageToStudent
字符串将初始化,但我想我错了。这是我到目前为止有:初始化字符串变量在if-else
if(school == 1) {
System.out.println(name + ", have you started your engineering project yet?");
taskCompleted = scan.nextLine().charAt(0);
if (taskCompleted == 'y' || taskCompleted == 'Y')
messageToStudent = "Good for you! Keep me updated on your progress.";
System.out.println(messageToStudent);
} else {
messageToStudent = "That's not good at all. It's nearly November!";
}
System.out.println(messageToStudent);
编译器不够智能。在声明变量时,给它一个初始值''“'或'null'。 – 2014-10-28 21:26:07
如果school是'1'而taskCompleted是'N',那么messageToStudent'初始化在哪里? – azurefrog 2014-10-28 21:26:12
你应该阅读[NullPointerException](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – gudthing 2014-10-28 21:27:50