亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

每次用戶鍵入“否”時,我將如何循環運行此代碼

每次用戶鍵入“否”時,我將如何循環運行此代碼

莫回無 2022-11-30 16:14:17
我正在編寫基于文本的冒險,但在運行類系統時遇到了問題。我已經設置了一個案例來運行將運行一個方法的代碼塊,但是案例和方法都在沒有用戶輸入的情況下運行不止一次,并且在用戶輸入后不會停止運行。我嘗試了 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}我希望該方法運行一次并且輸出是用戶想要的。
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

“我希望該方法運行一次并且輸出是用戶想要的?!?/p>

如果您的真正意思是,基本上這正是代碼的作用:

“我希望該方法運行一次,如果他或她輸入yes,輸出將是用戶希望的結果”。

如果用戶在classes()方法中對選擇輸入,則要求用戶選擇另一個類,直到他或她對選擇感到滿意為止。我相信這不是本意嗎?

我認為您的問題是,當用戶確實進行了類選擇時,它停留在classes()方法中。我相信您真正想要做的是返回該選擇并在最初調用 classes() 方法之后利用該選擇,以便更深入地進行游戲。

為了做到這一點,你需要你的classes()方法返回一些東西,最好是選擇的類。由于不同的類(等級)包含在一個字符串數組中:

static String[] Class = {"Farmer", "Wanderer", "Trader", "Nothing"};

您需要從classes()方法返回的只是所選類的實際索引值。您當然可以讓另一個類成員變量保存用戶選擇....也許是一個名為:userClass的變量。然而,在我們開始之前,我想至少提出兩個建議:

不要命名變量Class。你可以做到,但對我來說(我相信還有很多其他人)這很令人困惑而且不是一個好主意,因為class本身就是一個Java Keyword。作為建議,也許將其更改為:Ranks。

賦予您的變量和方法名稱含義,以便可以輕松理解變量可能包含的內容或方法將執行的操作,例如: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()方法名稱了嗎?遵循代碼并理解正在發生的事情要容易得多。不,我個人并不真正關心靜態變量和方法。靜態的確實有一席之地,但我更喜歡運行比這更嚴格的代碼。


您還會注意到上述代碼中的其他更改。特別是用于顯示類菜單選項的for循環:


[0]- Farmer

[1]- Wanderer

[2]- Trader

[3]- Nothing  

您使用一個名為counter的變量并將其用于索引。這正是您的for循環的用途。您可以擺脫counter變量并簡單地使用在for循環本身內聲明的變量。


注意switch/case塊是如何使用的。它如何減少代碼。您還會注意到getRank()方法返回用戶選擇的類的索引值。當從main()方法調用時, getRank()方法返回類索引值,我們將選擇的類放入成員變量userClass中,您現在可以在代碼中的任何地方使用它。是的,您也可以直接在getRank()方法中設置userClass變量。


查看完整回答
反對 回復 2022-11-30
  • 1 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號