我正在编写基于文本的冒险,但在运行类系统时遇到了问题。我已经设置了一个案例来运行将运行一个方法的代码块,但是案例和方法都在没有用户输入的情况下运行不止一次,并且在用户输入后不会停止运行。
我尝试了 for 循环、while 循环、do/while 循环,但没有任何深远的影响。我确实有一个理论,我需要在方法运行后实施案例更改,但我不知道如何在不自行更改案例的情况下执行此操作。
情况是这样的——
case "1.5":
System.out.println("\nNow, before we start you need to choose a class. Each one will offer diffrent benefits.");
classes();
break;
这是方法-
public static void classes() { //Class Method
counter = 0;
for (int a = 1; a <= Class.length; a++) { //Run the class array
System.out.println("[" + counter + "]- " + Class[counter]); //displays the class array
counter++;
}
do {
System.out.println("What would you like to be."); //ask for input
user = in .nextLine();
if (user.equals("0")) { //if input is this then ...
System.out.println("\nYour health will be increased by 1 permanently." +
"Are you sure you want to be a Farmer?");
user = in .nextLine();
}
if (user.equals("1")) { //if input is this then...
System.out.println("\nYou will have a dagger." +
"Are you sure you want to be a Wanderer?");
user = in .nextLine();
}
if (user.equals("2")) {
System.out.println("\nYou will have 1 health potion. Drink it to regain 3 HP." +
"Are you sure you want to be a Trader?");
user = in .nextLine();
}
if (user.equals("3")) {
System.out.println("You are just a normal human. You get nothing." +
"Are you sure you want to be nothing?");
user = in .nextLine();
}
} while(user.equalsIgnoreCase("no")); //runs while user types no
}
我希望该方法运行一次并且输出是用户想要的。
拉风的咖菲猫
相关分类