字符串if和else语句

时间:2016-02-11 16:00:19

标签: java string if-statement

以下代码不会按照预期的方式运行。输出始终是      "这不是一个有效的答案!"

我所指的代码如下:

System.out.println("Should you move along the road or continue through the forest?");
String anw = scan.next();
if (anw == "forest") {
    System.out.println("You decide to go to the forest, it's dark and creepy but it can't hurt, right?");
} else if (anw == "road") {
    System.out.println("You decided to go along the road and hope to find something, anything, there.");  
} else {
    System.out.println("That isn't a valid answer!");
} 

任何帮助都将不胜感激,谢谢!

0 个答案:

没有答案