文字冒险首选后退出?

时间:2014-08-26 21:49:08

标签: java java.util.scanner

我正在尝试制作一个简单的文字冒险游戏。游戏运行,但在您键入第一个选项后,它就像您完成一样退出程序。

这是我的代码:

import java.util.Scanner;

public class Adventure {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        String c1, c2, c3, c4, c5, c6, c7;

        System.out.println("What shop do you need to go to? the 'Armor Shop' or the 'Weapon   Shop'?");
        c1 = keyboard.next();
        System.out.println();

        if(c1 == "Armor Shop") {
            System.out.println("Do you need 'Light' or 'Heavy' armor?");
            c2 = keyboard.next();
            if(c2 == "Light") {
                System.out.println("Do you want 'Hide' or 'Leather'?");
                c4 = keyboard.next();
                if(c4 == "Hide") {
                    System.out.println("Here is your hide armor");
                }
                else if(c4 == "Leather") {
                    System.out.println("Here is your leather armor");
                }
                else if(c2 == "Heavy") {
                    System.out.println("Do you want 'Chain' or 'Iron'?");
                    c5 = keyboard.next();
                    if(c5 == "Chain") {
                        System.out.println("Here is your chain armor");
                    }
                    else if(c5 == "Iron") {
                        System.out.println("Here is your iron armor");
                    }
                }
            }
            else if(c1 == "Weapon Shop") {
                System.out.println("Do you need a 'Sword' or a 'Bow'?");
                c3 = keyboard.next();
                if(c3 == "Sword") {
                    System.out.println("Do you want a 'Longsword' or a 'Shortsword'?");
                    c6 = keyboard.next();
                    if(c6 == "Longsword") {
                        System.out.println("Here is your longsword");
                    }
                    else if(c6 == "Shortsword") {
                        System.out.println("Here is your shortsword");
                    }
                }
                else if(c3 == "Bow") {
                    System.out.println("Do you want 'Oak' or 'Elven'?");
                    c7 = keyboard.next();
                    if(c7 == "Oak") {
                        System.out.println("Here is your oak bow");
                    }
                    else if(c7 == "Elven") {
                        System.out.println("Here is your elven bow");
                    }
                }
            }
        }
    }
}

我是编码新手并且还在学习,所以任何帮助都会非常感激。我试着到处寻找答案但却找不到答案。

1 个答案:

答案 0 :(得分:0)

通过调用 java.util.Scanner.next(),您只需要在同一条目上调用下一个标记。

默认分隔符是空格,因此当您键入“ Armor Shop ”并输入将会发生的事情 Scanner.next()将返回“ Armor < / strong>“下一次调用 Scanner.next()将返回”购物“。

我认为这不是你想要的行为,所以你可以做的是调用 Scanner.nextLine(),这将返回所输入内容的全部内容,因此它会显示为“ 装甲店“并在下次通话时等待用户输入。