首页 > 解决方案 > 简单的冒险游戏产生错误的输出,编译时没有错误

问题描述

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

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);
                }
            }
        }
    }
}

标签: java

解决方案


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

  1. 对于next(),您正在扫描输入直到下一个空格,并将光标放在扫描停止的位置
  2. 对于nextLine(),您正在扫描输入直到行尾,然后将光标放在新行上。

免责声明:在继续阅读并查看建议的解决方案之前,您可以尝试使用上述信息找出您的代码有什么问题,看看您是否可以自己解决!:)

所以这个小错误发生在这一行:String inputName = keyboard.next();

例如,如果我要输入apogee您的应用程序然后按回车键,那么inputName将设​​置为apogee. 到目前为止看起来很棒,但是,回车键也被认为是输入。

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

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

  1. 紧随keyboard.nextLine()其后String inputName = keyboard.next();处理由回车键触发的输入(“\n”)
  2. 使用keyboard.nextLine()代替next()

快乐编码!


推荐阅读