gpt4 book ai didi

java - 解析错误;我已经尝试过,但无法弄清楚出了什么问题?

转载 作者:行者123 更新时间:2023-12-01 06:38:43 25 4
gpt4 key购买 nike

这段代码让我抓狂!!我已经花了一周时间试图找出问题所在。事实上,我的导师也无法在代码中找到问题 - 所以这非常令人沮丧......特别是因为它由于假定的解析问题而无法编译。
任何帮助都将非常令人惊奇并且非常感激! (如果不是太麻烦的话,你能向我解释一下你为什么要做出这样的调整吗?)

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...");

}
}
}

最佳答案

这个:

if (Wh.equals(("kitchen") && Kt.equals("refrigerator")))

应改为:

if (Wh.equals("kitchen") && Kt.equals("refrigerator"))

其他 if 语句也是如此。

此外,变量 WH2 从未初始化,当您对其调用 equals() 时,这会导致错误。

关于java - 解析错误;我已经尝试过,但无法弄清楚出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21881815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com