当用户输入第二个字符串时,它会执行第一个键入的字符串

时间:2015-02-24 15:48:38

标签: java

public static int atk = 5;

    public static void main(String[] args){
        System.out.println("DUNGEONS AND DWAGONS");
        Scanner scan = new Scanner(System.in);
        help();


        String input = scan.next();

    // when someone types help first it works, but when they type stats after it shows what would happen if someone typed help.     
        while(true){
            if (input.equals("help")){
                help();
                scan.next();

            }else if(input.equals("stats")){
                stats();
                scan.next();
            }
         }
    }
    static void help() {
        System.out.println("type n,s,e,w to move in a direction");
        System.out.println("type stats to see your stats");
        System.out.println("type look and then a direction n,e,s,w see the sights");
        System.out.println("if you wish to see this again type help");

    }

    static void stats(){
        System.out.println("Health " + health);
        System.out.println("Armour " + armour);
        System.out.println("Attack  " + atk);

    }

}

//我已经尝试了所有的东西,如果你输入第一个thig后输入任何东西,它会一遍又一遍地执行同样的事情。

1 个答案:

答案 0 :(得分:5)

将scan.next移到if / else之外,并将其分配给输入变量,如:

if (..) {
    help();
} else {
    stats();
}
 input = scan.next();

并期望退出,这样你就可以退出while循环并优雅地终止你的程序。