Java 当用户输入第二个字符串时,它将执行输入的第一个字符串
//我已经尝试了每一件事,如果在键入的第一件事之后键入任何内容,它将反复执行同一件事。只需将scan.next移到if/else之外,并将其分配给输入变量,如:Java 当用户输入第二个字符串时,它将执行输入的第一个字符串,java,Java,//我已经尝试了每一件事,如果在键入的第一件事之后键入任何内容,它将反复执行同一件事。只需将scan.next移到if/else之外,并将其分配给输入变量,如: public static int atk = 5; public static void main(String[] args){ System.out.println("DUNGEONS AND DWAGONS"); Scanner scan = new Scanner(System.in)
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);
}
}
并期待退出,这样您就可以走出while循环,优雅地终止您的程序。input是否有变化?@SotiriosDelimanolis正在引导您走向正确的方向。第一次之后,您没有将扫描仪读数分配给
输入
变量。如何解决此问题then@Jacob你可以接受答案,如果它有帮助,它将帮助其他用户在未来谁将面临同样的问题。
if (..) {
help();
} else {
stats();
}
input = scan.next();