THIS CODE IS DRIVING ME BONKERS!! I've spent a week now trying to figure out what is wrong. In fact, my instructor also could not find an issue within the code-so that's quite frustrating...especially because it won't compile due to a supposed parsing issue.
Any help would be more than amazing and very much appreciated!! (and if it's not too much of a bother, could you please explain to me why you make the adjustments that you do?)
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...");
}
}
}
This:
if (Wh.equals(("kitchen") && Kt.equals("refrigerator")))
Should be changed to:
if (Wh.equals("kitchen") && Kt.equals("refrigerator"))
Same thing for your other if
statements.
Also the variable WH2
is never initialized which will cause an error when you call equals()
on it.
您在每个零件上都有双括号-(Wh.equals((,Kt.equals((和Wh.equals((--带有结束号)))-每种情况下都应该只有1个括号。
Initalize the strings and make the changed as below. Otherwise you will get error Kt, yn, WH2 and ny as not initailized.
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...");
}
}
}
if (Wh.equals(("kitchen") && Kt.equals("refrigerator")))
It should be
if (Wh.equals(("kitchen") && Kt.equals("refrigerator")))
Same error for below two ifs
if (Kt.equals(("refrigerator") && yn.equals("no")))
if (Wh.equals(("kitchen") && Kt.equals("cabinet")))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.