我想用一个数字(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