简体   繁体   English

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

[英]Parsing error; I have tried and can't figure out what's wrong?

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. 您的其他if语句也是如此。

Also the variable WH2 is never initialized which will cause an error when you call equals() on it. 同样,变量WH2永远不会初始化,这将在您调用equals()时导致错误。

您在每个零件上都有双括号-(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. 否则,您将收到错误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...");

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

It should be 它应该是

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

Same error for below two ifs 以下两个if的相同错误

if (Kt.equals(("refrigerator") && yn.equals("no")))

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法弄清楚出了什么问题,出现了“类型已定义的错误” - I can't figure out what's wrong, getting a 'type already defined error' 无法弄清楚我的DocumentFilter有什么问题 - Can't figure out what's wrong with my DocumentFilter 我不知道怎么了,我的逻辑似乎正确 - I can't figure out what's wrong, my logic seems correct 我无法弄清楚我的NullPointerException有什么问题或者为什么它甚至存在 - I can't figure out what's wrong with my NullPointerException or why it even exists 我无法弄清楚比较数组索引有什么问题 - I can't figure out what's wrong with comparing array indexes 项目Euler#23(Java)。 我不知道怎么了。 答案是64 - Project Euler #23 (Java). I can't figure out what's wrong. Answer is off by 64 甚至 Java 中的阶乘方法。 我已经尝试了一切,但无法弄清楚 - Even Factorial Method in Java. I have tried everything and can't figure it out 我需要使用ArrayList类对数组进行排列。 我不知道怎么了 - I need to make permutations with an array using the ArrayList Class. I can't figure out what's wrong 无法找出导致此错误的原因“此类没有接受 String[] 的静态 void main 方法。” - Can't figure out what's causing this error "This class does not have a static void main method accepting String[]." 索引超出范围异常,但无法找出问题所在 - Index out of bounds Exception but can't figure out what is wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM