返回方法 Java 时,扫描器不更新输入
Scanner doesn't update input when returning to method Java
我正在写一个猜字游戏的代码。 main调用了inputTake方法,要求输入一个只有5个英文字母的单词,returns是。在返回单词之前,它会调用另一个方法 checkInput,以确保输入有效。如果输入无效,checkInput 方法会打印一条错误消息并调用 inputTake 让用户重试。
但是当第一个输入无效时,checkInput 调用 inputTake 然后第二个输入有效,一切似乎都正常。问题是方法 returns 第一个无效输入,而不是有效输入。
我尝试在 main 中初始化 Scanner 并将其作为参数提供给方法,但这没有帮助。
下面是我写的代码,有什么想法吗?欢迎任何帮助
主要:
Board board1 = new Board();
String guess = board1.inputTake();
董事会:
// take input - print a message and calls the checkInput method with the String inputed.
public String inputTake(){
Scanner scan = new Scanner(System.in);
String guess;
System.out.println("choose a word, pick carefully: ");
guess = scan.next();
// we gotta check whether the input's valid before we return it!
checkInput(guess);
return guess;
}
/* checks whether a given String is made out of 5 english language letters.
* if it is, program continues normally.
* if not, it prints error message and calls the InputTake method again.
*/
public void checkInput(String input) {
boolean isGood = true;
// check if 5 letters
if(input.length() != 5)
isGood = false;
// check if all are english
if(!input.matches("[a-zA-Z]+"))
isGood = false;
if(isGood == false) {
System.out.println("make sure your guess consists of 5 english letters, try again.");
inputTake();
}
}
如评论中所述,问题是您在 checkInput()
中的 inputTake()
调用没有按照您的意愿进行。你可以试试这个:
// take input - print a message and calls the checkInput method with the String inputed.
public String inputTake(){
Scanner scan = new Scanner(System.in);
String guess;
System.out.println("choose a word, pick carefully: ");
guess = scan.next();
// we gotta check whether the input's valid before we return it!
if(!isGoodInput(guess)) {
System.out.println("make sure your guess consists of 5 english letters, try again.");
guess = inputTake();
}
return guess;
}
/* checks whether a given String is made out of 5 english language letters.
* if it is, program continues normally.
*/
public boolean isGoodInput(String input) {
return input.length() == 5 && input.matches("[a-zA-Z]+");
}
我正在写一个猜字游戏的代码。 main调用了inputTake方法,要求输入一个只有5个英文字母的单词,returns是。在返回单词之前,它会调用另一个方法 checkInput,以确保输入有效。如果输入无效,checkInput 方法会打印一条错误消息并调用 inputTake 让用户重试。
但是当第一个输入无效时,checkInput 调用 inputTake 然后第二个输入有效,一切似乎都正常。问题是方法 returns 第一个无效输入,而不是有效输入。
我尝试在 main 中初始化 Scanner 并将其作为参数提供给方法,但这没有帮助。
下面是我写的代码,有什么想法吗?欢迎任何帮助
主要:
Board board1 = new Board();
String guess = board1.inputTake();
董事会:
// take input - print a message and calls the checkInput method with the String inputed.
public String inputTake(){
Scanner scan = new Scanner(System.in);
String guess;
System.out.println("choose a word, pick carefully: ");
guess = scan.next();
// we gotta check whether the input's valid before we return it!
checkInput(guess);
return guess;
}
/* checks whether a given String is made out of 5 english language letters.
* if it is, program continues normally.
* if not, it prints error message and calls the InputTake method again.
*/
public void checkInput(String input) {
boolean isGood = true;
// check if 5 letters
if(input.length() != 5)
isGood = false;
// check if all are english
if(!input.matches("[a-zA-Z]+"))
isGood = false;
if(isGood == false) {
System.out.println("make sure your guess consists of 5 english letters, try again.");
inputTake();
}
}
如评论中所述,问题是您在 checkInput()
中的 inputTake()
调用没有按照您的意愿进行。你可以试试这个:
// take input - print a message and calls the checkInput method with the String inputed.
public String inputTake(){
Scanner scan = new Scanner(System.in);
String guess;
System.out.println("choose a word, pick carefully: ");
guess = scan.next();
// we gotta check whether the input's valid before we return it!
if(!isGoodInput(guess)) {
System.out.println("make sure your guess consists of 5 english letters, try again.");
guess = inputTake();
}
return guess;
}
/* checks whether a given String is made out of 5 english language letters.
* if it is, program continues normally.
*/
public boolean isGoodInput(String input) {
return input.length() == 5 && input.matches("[a-zA-Z]+");
}