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變量。
添加回答
舉報