这个代码正在推动我的债券!我花了一个星期的时间试图找出问题所在。
实际上,我的教师也无法在代码中找到问题 - 因此非常令人沮丧...特别是因为它会因为假定的解析问题而无法编译。
任何帮助都会令人惊叹,非常感谢!! (如果不是太麻烦,你可以向我解释你为什么要进行调整吗?)
import java.util.Scanner;
public class ChooseYourOwnAdventure
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String Wh, Kt, yn, WH2, ny;
System.out.println("WELCOME TO MITCHELL'S TINY ADVENTURE!");
System.out.println("You are in a creepy house! Would you like to go \"upstairs\" or into the \"kitchen\"?");
Wh = keyboard.next();
{
if (Wh.equals("kitchen"))
{
System.out.println( "There is a long counter top with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the \"refrigerator\" or look in a \"cabinet\".");
Kt = keyboard.next();
}
if (Wh.equals(("kitchen") && Kt.equals ("refrigerator")))
{
System.out.println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food? (\"yes\" or \"no\") ");
yn = keyboard.next();
}
if (Kt.equals (("refrigerator") && yn.equals("no")))
{
System.out.println("You die of starvation...eventually.");
}
if (Wh.equals(("kitchen") && Kt.equals ("cabinet")))
{
System.out.println("Everything is rodent infested. Roaches everywhere! I think we even saw a rat!");
}
if (Wh.equals("upstairs"))
{
System.out.println("Upstairs you see a hallway. At the end of the hallway is the master \"bedroom\". There is also a \"bathroom\" off the hallway. Where would you like to go?");
WH2 = keyboard.next();
}
if (WH2.equals("bedroom"))
{
System.out.println("You are in a plush bedroom, with expensive-looking hardwood furniture. The bed is unmade. In the back of the room, the closet door is ajar. Would you like to open the door? (\"yes\" or \"no\")");
ny = keyboard.next();
}
if ((WH2.equals("bedroom") && (ny.equals ("No"))))
{
System.out.println("Well then I guess you'll never know what was in there. Thanks for playing, I'm tired of making nested if statements.");
}
if (WH2.equals("bathroom"))
{
System.out.println("It stinks in there! We are going back to the hallway!");
}
System.out.println("Thanks for playing...");
}
}
}
答案 0 :(得分:10)
此:
if (Wh.equals(("kitchen") && Kt.equals("refrigerator")))
应改为:
if (Wh.equals("kitchen") && Kt.equals("refrigerator"))
您的其他if
语句也是如此。
此外,变量WH2
永远不会被初始化,当您在其上调用equals()
时会导致错误。
答案 1 :(得分:1)
你有双括号 - (每个部分的Wh.equals((,Kt.equals((和Wh.equals(( - 关闭)) - 在每种情况下它应该只有1个括号。
答案 2 :(得分:0)
初始化字符串并进行如下更改。否则,您将得到错误Kt,yn,WH2和ny未初始化。
public class ChooseYourOwnAdventure
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String Wh="", Kt="", yn="", WH2="", ny="";
System.out.println("WELCOME TO MITCHELL'S TINY ADVENTURE!");
System.out.println("You are in a creepy house! Would you like to go \"upstairs\" or into the \"kitchen\"?");
Wh = keyboard.next();
{
if (Wh.equals("kitchen"))
{
System.out.println( "There is a long counter top with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the \"refrigerator\" or look in a \"cabinet\".");
Kt = keyboard.next();
}
if (Wh.equals("kitchen") && Kt.equals ("refrigerator"))
{
System.out.println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food? (\"yes\" or \"no\") ");
yn = keyboard.next();
}
if (Kt.equals ("refrigerator") && yn.equals("no"))
{
System.out.println("You die of starvation...eventually.");
}
if (Wh.equals("kitchen") && Kt.equals ("cabinet"))
{
System.out.println("Everything is rodent infested. Roaches everywhere! I think we even saw a rat!");
}
if (Wh.equals("upstairs"))
{
System.out.println("Upstairs you see a hallway. At the end of the hallway is the master \"bedroom\". There is also a \"bathroom\" off the hallway. Where would you like to go?");
WH2 = keyboard.next();
}
if (WH2.equals("bedroom"))
{
System.out.println("You are in a plush bedroom, with expensive-looking hardwood furniture. The bed is unmade. In the back of the room, the closet door is ajar. Would you like to open the door? (\"yes\" or \"no\")");
ny = keyboard.next();
}
if ((WH2.equals("bedroom") && (ny.equals ("No"))))
{
System.out.println("Well then I guess you'll never know what was in there. Thanks for playing, I'm tired of making nested if statements.");
}
if (WH2.equals("bathroom"))
{
System.out.println("It stinks in there! We are going back to the hallway!");
}
System.out.println("Thanks for playing...");
}
}
}
答案 3 :(得分:0)
if (Wh.equals(("kitchen") && Kt.equals("refrigerator")))
应该是
if (Wh.equals(("kitchen") && Kt.equals("refrigerator")))
以下两个ifs
的错误相同if (Kt.equals(("refrigerator") && yn.equals("no")))
if (Wh.equals(("kitchen") && Kt.equals("cabinet")))