gpt4 book ai didi

java - 如何让扫描仪输入不触发第三种方法?

转载 作者:行者123 更新时间:2023-11-30 05:40:58 25 4
gpt4 key购买 nike

我正在做一项作业,大部分已经完成,但我对最后一种方法有疑问。我正在尝试编写一个 continueGame() 方法,该方法将询问用户是否要继续玩,并接受“y”或“n”。如果回答“y”,程序将再次启动。如果回答“n”,程序将停止并显示一条消息。问题是我需要它仅在 userChoice == answer 时触发 continueGame() 方法。这是一个采用面向对象方法的猜数字游戏。

我尝试在 else if(userChoice ==answer) 语句中调用 continueGame() 方法,但它似乎不起作用。即使触发了其他 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”

最佳答案

驱动程序类在 while 循环内调用 continueGame()。如果您不允许修改该类,那么每次迭代时询问可能是预期的行为。

您应该将 System.out.println("Would you like to play again? (y/n)"); 移至 continueGame() 方法中,以便它只询问何时调用该方法。

关于java - 如何让扫描仪输入不触发第三种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55680846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com