【问题标题】:Parsing error; I have tried and can't figure out what's wrong?解析错误;我试过了,不知道怎么回事?
【发布时间】:2014-02-19 13:23:23
【问题描述】:

这段代码让我发疯了!!我已经花了一个星期的时间试图找出问题所在。 事实上,我的导师也找不到代码中的问题——所以这很令人沮丧……尤其是因为假定的解析问题而无法编译。
任何帮助都会令人惊叹,非常感谢! (如果不是太麻烦的话,请您向我解释一下您为什么要进行这些调整?)

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

        }
    }
}

【问题讨论】:

  • 能否请您提供堆栈跟踪或评论导致编译问题的行?
  • 您收到的确切信息是什么?

标签: java parsing java.util.scanner


【解决方案1】:

这个:

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

应改为:

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

对于您的其他 if 语句也是如此。

另外,变量WH2 永远不会被初始化,这会在你调用equals() 时导致错误。

【讨论】:

  • 为你的鹰眼+1。或者使用 IDE 告诉您括号不匹配的位置。
  • 我必须承认我作弊并使用 IDE 来捕获错误 :)
  • @Petter 这不是作弊,这是在利用有用的工具! :D
【解决方案2】:

每个部分都有双括号 - (Wh.equals((, Kt.equals (( 和 Wh.equals(( - 结束))) - 在每种情况下它应该只有一个括号。

【讨论】:

  • 感谢您为我们阅读其他答案...或者您只是打字速度很慢?
  • @Jan Dvorak Lol - 哦,是的 - 起步较晚 ;)
【解决方案3】:

初始化字符串并进行如下更改。否则你会得到错误 Kt, yn, WH2 and ny as not initialized。

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

    }
}
}

【讨论】:

    【解决方案4】:
    if (Wh.equals(("kitchen") && Kt.equals("refrigerator")))
    

    应该是

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

    以下两个 if 的错误相同

    if (Kt.equals(("refrigerator") && yn.equals("no")))
    
    if (Wh.equals(("kitchen") && Kt.equals("cabinet")))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      相关资源
      最近更新 更多