我想用一個數字(1/2/3)的用戶輸入來控制某些東西。當我嘗試使用轉換的選擇時,它說找不到符號。爲什麼是這樣?爲什麼我不能使用這個變量
// Start user input //
public static int userChoice() {
Scanner userInput = new Scanner(System.in);
String choice = userInput.nextLine();
int convertedChoice = Integer.parseInt(choice);
return convertedChoice;
}
// End user input //
// Start scan maze //
static void whatMaze() {
if (convertedChoice == 1) {
System.out.println("you chose 1");
} else {
System.out.println("you chose something else");
}
}
您已經定義了'convertedChoice'()'方法,你不能從'whatMaze()'中使用它。 – Kayaman
我以爲我將userChoice設置爲public,並且返回convertedChoice將允許我這樣做? –
userChoice是一種方法,所以它是公開的,只允許你從任何地方調用它。您可能需要從該方法返回值,或將所有這些方法放入具有選擇屬性的類中。我建議前者。 – abalos