java - 如何让扫描仪输入不触发第三种方法?
问题描述
我正在处理一项任务,并且大部分时间都完成了,但是我对最后一种方法有疑问。我正在尝试编写一个continueGame()
方法来询问用户是否要继续玩,并接受“y”或“n”。如果回答“y”,程序将再次启动。如果回答“n”,则程序停止并显示一条消息。问题是我需要它continueGame()
仅在userChoice == answer
. 这是一款采用面向对象方法的数字猜谜游戏。
我试图continueGame()
在我的语句中调用该方法,else if(userChoice == answer)
但它似乎不起作用。即使if/else if
触发了我的其他语句,它也会继续该continueGame()
方法。
这是游戏的主要驱动程序
import java.util.Scanner;
public class NumberGame
{
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
GameOptions opt = new GameOptions(); // Your created class
int userChoice = -1234;
int answer = -1234;
boolean keepPlaying = true;
System.out.println("Guess the Number Game\n");
while (keepPlaying == true) {
answer = (int) (Math.random() * 10)+1;
//Create a getChoice method in your class and make sure it accepts a Scanner argument
userChoice = opt.getChoice(input);
//Create a checkAnswer method in your class. Make sure it accepts two integer arguments and a Scanner argument
opt.checkAnswer(userChoice, answer, input);
// Create a continueGame method in your class and make sure it accepts a Scanner argument
keepPlaying = opt.continueGame(input);
}
System.out.println("Thanks for playing.");
}
}
这是我正在为这些方法工作的课程。请注意,我不能对主驱动程序文件进行任何修改。
import java.util.InputMismatchException;
import java.util.Scanner;
import java.lang.NumberFormatException;
public class GameOptions {
int count = 0;
boolean cont = true;
//getChoice Method for NumberGame
public int getChoice(Scanner scnr) {
System.out.println("Please choose a number between 1 and 10: ");
int userGuess = 0;
String input = scnr.next();
try {
userGuess = Integer.parseInt(input);
if (userGuess < 1 || userGuess > 10) {
throw new IllegalArgumentException("Invalid value. Please enter a number between 1 and 10: ");
}
}
catch(NumberFormatException e) {
System.out.println("Error - Enter Numerical Values Only");
return userGuess;
}
catch (IllegalArgumentException ex) {
System.out.println(ex.getMessage());
}
return Integer.parseInt(input);
}
public void checkAnswer(int userChoice, int answer, Scanner scnr) {
if (userChoice > answer && userChoice < 11) {
System.out.println("Too high. Try again.");
count++;
} else if (userChoice < answer && userChoice > 0) {
System.out.println("Too low. Try again.");
count++;
} else if (userChoice == answer) {
System.out.println("You got it! Number of tries: " + count);
System.out.println("Would you like to play again? (y/n)");
}
}
public static boolean continueGame(Scanner scnr) {
String input = scnr.nextLine();
if (input.toLowerCase().equals("y")){
return true;
} else if (input.toLowerCase().equals("n")){
return false;
} else {
System.out.println("Invalid entry. Please enter either y or n: ");
return continueGame(scnr);
}
}
}
所以我应该可以输入一个数字,如果它低于答案它会告诉我我太低,如果它高于答案它会告诉我它太高,如果它相等它会告诉我我赢了如果我想继续,提示我按“y”或“n”。我遇到的另一个问题是我得到“你想再玩一次吗?(y/n)”无论我猜对与否,我唯一的选择是点击“y”或“n”
解决方案
驱动程序类continueGame()
在 while 循环内调用。如果您不允许修改该类,那么大概在每次迭代时询问是预期的行为。
您应该System.out.println("Would you like to play again? (y/n)");
进入该continueGame()
方法,以便它只询问何时调用该方法。
推荐阅读
- c++ - 如何将 pls 文件保存在 QMediaPlaylist 而不是 m3u 中
- ios - TableViewController 中的自定义 UIView
- angular - Angular Material Tabs Wrapper - 添加新选项卡不会反映在 UI 中
- python - Python - 通过特定 IP 地址路由 selenium 浏览器
- c++ - compiling from headers
, are not giving any output - r - 如何使用 readr 为类似命名的列定义列规范?
- java - 进度条 Java servlet 和 jsp
- gitlab - 如何在 Gitlab 中完成工作并发出警告
- php - Amazon S3 PHP:获得成功复制对象的确认
- c++ - QTcpServer/QTcpSocket:使用 QDataStream 与直接发送 UTF-8 数据