我一直在尝试用Java实现石头,纸,剪刀的游戏。 当我尝试使用getinput方法时:第一次尝试返回正确的输出1、2或3(在gamelogic类中,rock,paper,scissor声明为static final ..) 但是当我输入不正确的输入然后正确输入时,它总是返回0!
public int getInput(){
System.out.println("Select ROCK , PAPER or SCISSOR");
String choice = scanner.nextLine();
choice = choice.toUpperCase();
char c = choice.charAt(0);
if(c == 'R'){
return gameLogic.rock;
}else if(c == 'P'){
return gameLogic.paper;
}else if(c == 'C'){
return gameLogic.scissor;
}
getInput();
return 0;
}
答案 0 :(得分:0)
尝试
public static int getInput(){
int result = 0;
System.out.println("Select ROCK , PAPER or SCISSOR");
Scanner scanner = new Scanner(System.in);
String choice = scanner.nextLine();
choice = choice.toUpperCase();
char c = choice.charAt(0);
if(c == 'R'){
result = 1;
}else if(c == 'P'){
result = 2;
}else if(c == 'C'){
result = 3;
} else {
return getInput();
}
return result;
}