每次用户键入“否”时,我将如何循环运行此代码

我正在编写基于文本的冒险,但在运行类系统时遇到了问题。我已经设置了一个案例来运行将运行一个方法的代码块,但是案例和方法都在没有用户输入的情况下运行不止一次,并且在用户输入后不会停止运行。


我尝试了 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


}

我希望该方法运行一次并且输出是用户想要的。


莫回无
浏览 81回答 1
1回答

拉风的咖菲猫

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

相关分类

Java