如果您插入无效输入,即构建代码猜测游戏,即(333)它将提示您更改猜测。但这仅适用于猜测#2 - #6的猜测#1,它会让任何无效输入通过
public void game(){
System.out.println("Enter guess #" + (guessAtt + 1));
guess = keyboard.next();
guess = guess.toLowerCase();
if( guess.equals(quit)){
System.exit(0);
}
if (guess.length() < 2){
System.out.println("Guess Too short try again");
game();
}
if (guess.length() > 3){
System.out.println("Guess too long try again");
game();
}
letter1 = guess.charAt(0);
letter2 = guess.charAt(1);
letter3 = guess.charAt(2);
isValid();
}
public boolean isValid(){
if (letter1.equals('a')|| letter1.equals('b')|| letter1.equals('c')|| letter1.equals('d')|| letter1.equals('e')){
isValid1 = true;
}
if(letter2.equals('a')|| letter2.equals('b')|| letter2.equals('c')|| letter2.equals('d')|| letter2.equals('e')){
isValid2 = true;
}
if(letter3.equals('a')|| letter3.equals('b')|| letter3.equals('c')|| letter3.equals('d')|| letter3.equals('e')){
isValid3 = true;
}
if(isValid1 == true && isValid2 == true && isValid3 == true){
isValid = true;
}
else {
isValid = false;
}
while (isValid == false){
System.out.println("invalid input try again\n");
game();
}
return isValid;
}
答案 0 :(得分:2)
你可以在游戏中使用while循环,当isValid()返回true时会中断。如果isValid()返回false值,您也可以调用函数游戏。现在你要求一个布尔值,但你不能使用它。无论它返回什么,只要你的值包含正确的长度,游戏就会结束。