我正在完成一项任务,我主要完成了它,但我遇到了最后一个方法的问题。我正在尝试编写一个continueGame()
方法,询问用户是否要继续播放,并接受“y”或“n”。如果回答“是”,则程序再次启动。如果回答“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”
编写驱动程序的方式(我猜)来自您的讲师/讲师/教授,对吧?
使用驱动程序(原样),您不需要从checkAnswer方法调用continueGame方法。司机打算打电话给它。
只需运行驱动程序即可。如果您有一个合适的IDE(eclipse或Netbeans),请追踪并查看所接受的输入(我认为在接受的答案中有换行符)。
试试这个(我刚改变了循环结构;你的也是有效的):
public static boolean continueGame(Scanner scnr) {
while (true) {
String input = scnr.nextLine().trim(); // to remove white spaces and line-feed
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: ");
}
}
}
添加了checkAnswer方法,以保持用户猜测答案,直到他得到正确答案:
public static checkAnswer(/*three arguments*/) {
boolean correct = false;
while (! correct) {
// accept input
if (answer.equals(input)) {
correct = true;
// print required correct/congrats messages here
} else {
// print required input/try again messages here
}
}
// print would you like to play again with new answer y/n message here.
}
在我看来,打印“再次播放新答案y / n消息”应该进入continueGame方法(从checkAnswer的最后一部分)方法,以坚持封装概念。
驱动程序类在while循环中调用continueGame()
。如果您不允许修改该类,那么可能在每次迭代时询问是否是预期的行为。
你应该将System.out.println("Would you like to play again? (y/n)");
移动到continueGame()
方法中,以便它只询问何时调用该方法。