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

标签 java

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

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() 的使用可能有些混淆。

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

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

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

例如,如果我在您的应用程序中键入 apogee,然后按回车键,则 inputName 将设置为 apogee。直到现在看起来还不错,但是,回车键也被认为是一种输入。

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

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

  1. String inputName = keyboard.next(); 之后添加 keyboard.nextLine() 以处理回车键 ("\n") 触发的输入
  2. 使用 keyboard.nextLine() 而不是 next()

编码愉快!

关于java - 简单的冒险游戏产生错误的输出,编译时没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52019291/

相关文章:

Java 字符串参数

java - 计算以相同子串开头和结尾的最大子串的长度

java - 如何在 Maven 中处理 "incompatible"JDK?

java - cometd 通过 websocket : "unknown bayeux transport"

java - Java中生成不同位数的随机数(位数将与用户输入的数字一样多)

java - 打开 pdf 时出现 NullPointer 异常 (MUPDF Android)

java - 为什么我无法在运行 JAR 之前加载所需的库?

java - 在 JSP 中,context.xml 似乎被破坏错误

java - 在 ArrayList 中搜索对象

java - Swing 灯箱效果