Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 简单的冒险游戏产生错误的输出,编译时没有错误_Java - Fatal编程技术网

Java 简单的冒险游戏产生错误的输出,编译时没有错误

Java 简单的冒险游戏产生错误的输出,编译时没有错误,java,Java,编译时我没有收到任何错误,但是输出不正确? 当用户应该能够输入时,程序停止 import java.util.Scanner; class person { private String name, choice1, choice2, choice3; //Getters public String getName() { return this.name; } public String getChoice1() {

编译时我没有收到任何错误,但是输出不正确? 当用户应该能够输入时,程序停止

import java.util.Scanner;
class person
{
    private String name, choice1, choice2, choice3;

    //Getters
    public String getName()
    {
        return this.name;
    }
    public String getChoice1()
    {
        return this.choice1;
    }
    public String getChoice2()
    {
        return this.choice2;
    }
    public String getChoice3()
    {
        return this.choice3;
    }

    //Setters
    public void setName( String n )
    {
        this.name = n;
    }
    public void setChoice1( String c1 )
    {
        this.choice1 = c1;
    }
    public void setChoice2( String c2 )
    {
        this.choice2 = c2;
    }
    public void setChoice3( String c3 )
    {
        this.choice3 = c3;
    }
}
public class AdventureGame
{
    public static void main(String[] Args) throws Exception
    {
        String end = "Game Over";
        Scanner keyboard = new Scanner(System.in);
        person p = new person();
        //intro
        System.out.println( "Welcome travellar, your adventure awaits you..." );
        Thread.sleep(1000);
        System.out.print( "Player Name: " );
        String inputName = keyboard.next();
        p.setName(inputName);
        Thread.sleep(1000);
        //Question 1
        System.out.println( "You are in a creepy house! Would you like to go \"upstairs\" or into the \"kitchen\"?" );
        String inputChoice1 = keyboard.nextLine();
        p.setChoice1(inputChoice1);

        if ( p.getChoice1().equals("kitchen") )
        {   //Q2
            System.out.println("There is a long countertop with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the \"refridgerator\" or look in a \"cabinet\" ");
            String inputChoice2 = keyboard.nextLine();
            p.setChoice2(inputChoice2);
            if ( p.getChoice2().equals("refridgerator") )
            {
                System.out.println("Inside the refridgerator you see some food. Would you like to eat that food? (\"yes\" or \"no\")");
                String inputChoice3 = keyboard.nextLine();
                p.setChoice3(inputChoice3);
                if ( p.getChoice3().equals("yes") )
                {
                    System.out.println(p.getName() + " died of food poisoning.");
                    Thread.sleep(1000);
                    System.out.print(end);
                }
                else if ( p.getChoice3().equals("no") )
                {
                    System.out.print("You will never know what that food tasted like. The regret haunts you till suicide.");
                    Thread.sleep(1000);
                    System.out.print(end);
                }
            }
            else if ( p.getChoice2().equals("cabinet") )
            {   
                System.out.println( "The cabinet was a trap! You took a barbed contraption to the face; you are blinded and bleeding out." );
                Thread.sleep(1000);
                System.out.println("As you lie on the floor blind and bleeding out, you hear footsteps. Do you \"move\" or try to \"hide\"?");
                String inputChoice3 = keyboard.nextLine();
                p.setChoice3(inputChoice3);
                if ( p.getChoice3().equals("move") )
                {
                    System.out.print("As you moved faster so did the footsteps. you took a final blow.");
                    Thread.sleep(1000);
                    System.out.print(end);
                }
                else if ( p.getChoice3().equals("hide") )
                {
                    System.out.println("blinded, you failed to realise that you were in plain sight. You became an easy meal.");
                    Thread.sleep(1000);
                    System.out.print(end);
                }


            }

        }
        else if ( p.getChoice1().equals("upstairs") )
        {
            System.out.println("As you reach the top of the stairs, your encounter 3 doors; which door do you enter? (\"1\" or \"2\")");
            String inputChoice2 = keyboard.nextLine();
            p.setChoice2(inputChoice2);
            if ( p.getChoice2().equals("1") )
            {
                System.out.println("As you grab the handle of the first of the door you hear a scream!");
                Thread.sleep(1000);
                System.out.println("do you \"run\" or \"open\" the door?!");
                String inputChoice3 = keyboard.nextLine();
                p.setChoice3(inputChoice3);

                if( p.getChoice3().equals("run") )
                {
                    System.out.println("Running down the stairs, you misplaced a foot and fell to your demise.");
                    Thread.sleep(1000);
                    System.out.print(end);
                }
                else if (p.getChoice3().equals("open"))
                {
                    System.out.println("As your head poked through, an unrelenting force slammed the door, decapitating your head.");
                    Thread.sleep(1000);
                    System.out.print(end);
                }
            }
            else if ( p.getChoice2().equals("2") )
            {
                System.out.println("You enter what seems to be a vacant bedroom. Do you take a nap? (\"yes\" or \"no\")");
                String inputChoice3 = keyboard.nextLine();
                p.setChoice3(inputChoice3);
                if ( p.getChoice3().equals("yes") )
                {
                    System.out.println("You never wake up...");
                    System.out.print(end);
                }
                else if ( p.getChoice3().equals("no") )
                {
                    System.out.println("You turn around turn around to leave bu the door is gone?!");
                    Thread.sleep(1000);
                    System.out.println( "The grim reaper appears... Your time has come." );
                    Thread.sleep(1000);
                    System.out.print(end);
                }
            }
        }
    }
}

关于
next()
nextLine()
的使用可能会有一些混淆

  • 对于
    next()
    ,您将扫描输入直到下一个空格,并将光标放置在扫描停止的位置
  • 对于
    nextLine()
  • 免责声明:在继续阅读并查看建议的解决方案之前,您可以尝试使用上述信息找出代码中的错误,并看看是否可以自己解决它!:)

    因此,这一行出现了一个小错误:
    String inputName=keyboard.next()

    例如,如果我在应用程序中键入
    apogee
    ,然后按enter键,那么
    inputName
    将设置为
    apogee
    。到目前为止,这似乎还不错,但回车键也被视为一种输入

    现在,当我们到达
    String inputChoice1=keyboard.nextLine()
    ,enter键将被视为输入,而您的
    inputChoice1
    现在是一个空字符串。由于没有与空字符串匹配的条件,因此将退出应用程序

    有两种方法可以解决此问题:

  • String inputName=keyboard.next()之后添加
    keyboard.nextLine()
    处理由enter键(“\n”)触发的输入
  • 使用
    keyboard.nextLine()
    而不是
    next()

  • 快乐编码

    关于
    next()
    nextLine()
    的使用可能会有一些混淆

  • 对于
    next()
    ,您将扫描输入直到下一个空格,并将光标放置在扫描停止的位置
  • 对于
    nextLine()
  • 免责声明:在继续阅读并查看建议的解决方案之前,您可以尝试使用上述信息找出代码中的错误,并看看是否可以自己解决它!:)

    因此,这一行出现了一个小错误:
    String inputName=keyboard.next()

    例如,如果我在应用程序中键入
    apogee
    ,然后按enter键,那么
    inputName
    将设置为
    apogee
    。到目前为止,这似乎还不错,但回车键也被视为一种输入

    现在,当我们到达
    String inputChoice1=keyboard.nextLine()
    ,enter键将被视为输入,而您的
    inputChoice1
    现在是一个空字符串。由于没有与空字符串匹配的条件,因此将退出应用程序

    有两种方法可以解决此问题:

  • String inputName=keyboard.next()之后添加
    keyboard.nextLine()
    处理由enter键(“\n”)触发的输入
  • 使用
    keyboard.nextLine()
    而不是
    next()
  • 快乐编码