我正在编码基于文本的冒险,并遇到了运行类系统的问题。我已经设置了一个案例来运行将运行方法的代码块,但是情况和方法都在没有用户输入的情况下运行了不止一次,并且不会在用户输入后停止运行。
我尝试了一个循环,一段时间的循环,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
}
我希望该方法运行一次,并且输出是用户想要的。
"我希望该方法运行一次,输出是用户 想要它。"
基本上,这正是代码所做的,如果您的真正含义:
"我希望该方法运行一次,输出是用户 希望他或她输入是。
如果用户输入 no 对于 class((方法中的选择,则要求用户选择另一个类,直到他或她对选择满意。我相信这不是吗?
我认为您的问题是,当用户确实进行类选择时,它将停留在 class((方法中。我认为您真正想做的是返回选择并在之后使用选择最初称为 class((方法为了进一步加入游戏。
为了做到这一点,您需要 class((方法才能返回某物,最好是选择的类。由于不同类(等级(包含在字符串数组中:
static String[] Class = {"Farmer", "Wanderer", "Trader", "Nothing"};
您需要从 class((方法返回的所有需要是所选类的实际索引值。当然,您可以让另一个类成员变量保留用户选择。...也许是一个名为: USERCLASS 的变量。但是,在我们介入之前,我想提出至少两个建议:
不要命名变量 class 。您可以做到这一点,但对我来说(我敢肯定很多其他(这是令人困惑的,这并不是一个好主意,因为 class 本身是Java关键字。作为建议,也许将其更改为以下内容:等级。
给出您的变量和方法名称的含义,以便可以轻松理解变量可能存在的内容或该方法会喜欢什么: getRank((。
// Class Member Variables
static Scanner keyBoardInput = new Scanner(System.in);
static String userInput;
static String userClass = "";
static String[] RANKS = {"Farmer", "Wanderer", "Trader", "Nothing"};
static String LS = System.lineSeparator();
public static void main(String args[]) {
System.out.println("nNow, before we start you need to choose a class." + LS
+ "Each Class will offer diffrent benefits.");
int rank = getRank();
userClass = RANKS[rank];
System.out.println("Your chosen Class is: " + userClass);
}
public static int getRank() {
int maxRank = (RANKS.length - 1);
// Displays the RANKS array
for (int a = 0; a < RANKS.length; a++) {
System.out.println("[" + a + "]- " + RANKS[a]);
}
// Allow User to select a Rank
int rank = -1;
do {
System.out.println("What would you like to be? (0 to " + maxRank +
" or q to quit)"); //ask for input
userInput = keyBoardInput.nextLine();
// Quit game if desired.
if (userInput.equalsIgnoreCase("q")) {
System.out.println(LS + "Thanks for playing...Bye Bye.");
System.exit(0);
}
// Make sure a valid numerical value was supplied!
if (!userInput.matches("\d") || Integer.parseInt(userInput) < 0 ||
Integer.parseInt(userInput) > maxRank) {
System.out.println("Invalid input! You can only supply a value "
+ "from 0 to " + maxRank);
userInput = "no";
continue;
}
rank = Integer.parseInt(userInput); // Store for for array index use and returning
String message = "";
// Farmer (0)
switch (rank) {
case 0:
message = LS + "Your health will be increased by 1 permanently.";
break;
case 1:
//if input is this then...
message = LS + "You will have a dagger.";
break;
case 2:
message = LS + "You will have 1 health potion. Drink it to regain 3 HP.";
break;
case 3:
message = LS + "You are just a normal human. You get nothing.";
break;
default:
break;
}
System.out.println(message);
System.out.println("Are you sure you want to be " + RANKS[rank] + "?");
userInput = keyBoardInput.nextLine();
} while (userInput.equalsIgnoreCase("no")); //loops while user types no
return rank; // Return the index value for the chosen Rank.
}
注意变量名称和 getRank((方法名称?遵循代码并了解正在发生的事情要容易得多。不,我个人并不真正在乎静态变量和方法。静态确实有一个位置,但我更喜欢在比这更严格的情况下运行代码。
您还会注意到上述代码中的其他更改。特别是,用于用于显示类菜单的循环:
[0]- Farmer
[1]- Wanderer
[2]- Trader
[3]- Nothing
您使用一个名为 counter 的变量并将其用于索引。这正是您的 for 循环的目的。您可以摆脱 counter 变量,并简单地利用 a 在 loop本身中声明的变量
请注意如何使用 switch/case 块。它如何减少代码。您还将注意到 getRank((方法返回用户选择的类的索引值。当从 main((中调用时,方法 getRank((方法返回类索引值,我们将选择的类放入成员变量 USERCERC >您现在可以在代码中的任何地方使用它。是的,您可以直接在 getRank((方法中设置 USERCLASS 变量。