Java 如何使扫描仪输入不触发第三种方法?

Java 如何使扫描仪输入不触发第三种方法?,java,Java,我正在做一项作业,我大部分时间都完成了,但最后一种方法有问题。我正在尝试编写一个continueGame()方法,询问用户是否希望继续播放,并接受“y”或“n”。如果回答“y”,程序将再次启动。如果回答“n”,则程序停止并显示一条消息。问题是,只有当userChoice==answer时,我才需要它来触发continueGame()方法。这是一个使用面向对象方法的数字猜测游戏 我尝试在我的else if(userChoice==answer)语句中调用continueGame()方法,但它似乎不

我正在做一项作业,我大部分时间都完成了,但最后一种方法有问题。我正在尝试编写一个
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);
        }
    }
}
import java.util.InputMismatchException;
导入java.util.Scanner;
导入java.lang.NumberFormatException;
公共类游戏选项{
整数计数=0;
布尔控制=真;
//NumberGame的getChoice方法
公共int getChoice(扫描仪序列号){
System.out.println(“请选择一个介于1和10之间的数字:”);
int userGuess=0;
字符串输入=scnr.next();
试一试{
userGuess=Integer.parseInt(输入);
如果(用户猜测<1 | |用户猜测>10){
抛出新的IllegalArgumentException(“无效值。请输入一个介于1和10之间的数字:”);
}
}
捕获(数字格式){
System.out.println(“错误-仅输入数值”);
返回userGuess;
}
捕获(IllegalArgumentException ex){
System.out.println(例如getMessage());
}
返回整数.parseInt(输入);
}
公共无效检查应答(int userChoice、int answer、Scanner scnr){
if(userChoice>answer&&userChoice<11){
System.out.println(“太高,请重试”);
计数++;
}else if(userChoice0){
System.out.println(“太低,请重试”);
计数++;
}else if(userChoice==答案){
System.out.println(“成功了!尝试次数:“+count”);
System.out.println(“您想再次播放吗?(y/n)”);
}
}
公共静态布尔continueGame(扫描仪序列号){
字符串输入=scnr.nextLine();
if(input.toLowerCase().equals(“y”)){
返回true;
}else if(input.toLowerCase().equals(“n”)){
返回false;
}否则{
System.out.println(“输入无效。请输入y或n:”);
返回连续名称(scnr);
}
}
}

所以我应该能够输入一个数字,如果它低于答案,它会告诉我我太低,如果它高于答案,它会告诉我它太高,如果它等于,它会告诉我我赢了,如果我想继续,它会提示我按“y”或“n”。我遇到的另一个问题是,无论我猜的数字是否正确,我都会得到“你想再玩一次吗?(y/n)”,我唯一的选择是点击“y”或“n”

驱动程序类正在while循环中调用
continueGame()
。如果您不允许修改该类,那么在每次迭代时询问大概是您想要的行为


您应该移动
System.out.println(“您想再次播放吗?(y/n)”)编码到
continueGame()
方法中,这样它只会询问何时调用该方法。

驱动程序的编写方式(我猜)来自您的讲师/教授,对吗

对于驱动程序(实际上),您不需要从checkAnswer方法调用continueGame方法。司机会叫它的

只要运行驱动程序,它就会工作。如果您有一个合适的IDE(eclipse或Netbeans),请跟踪并查看接受的输入是什么(我认为在接受的答案中有换行符)

试试这个(我刚刚更改了循环结构;您的也是有效的):

为checkAnswer方法添加了一个选项,用于让用户猜测答案,直到得到正确答案:

publicstaticcheckanswer(/*三个参数*/){
布尔正确=错误;
而(!正确){
//接受输入
如果(回答等于(输入)){
正确=正确;
//在此处打印所需的正确/祝贺消息
}否则{
//在此处打印所需的输入/重试消息
}
}
//打印您是否想在此处再次播放新答案y/n消息。
}

在我看来,打印“再次使用新答案y/n消息”应该进入continueGame方法(来自checkAnswer的最后一部分)以坚持封装概念

嗯,我可以试试。唯一的问题是,该程序的工作方式是,它只应在userChoice==answer之后询问您是否想再次播放。下面是一个示例,演示了如果编码正确,程序应该是什么样子。猜猜数字游戏请选择一个介于1和10之间的数字:10太高。再试一次。请选择一个介于1和10之间的数字:5太低。再试一次。请选择一个介于1和10:8之间的数字,你知道了!尝试次数:3您想再次玩吗?(是/否):k无效条目。请输入Y或N:Y唯一的方法是在
GameOptions
类中添加一个布尔值,当它们得到答案时设置为
true
。然后在
continueGame()
方法中检查该布尔值是否为真-如果为“继续播放spiel”,如果不是,则返回真。@Matt。。。您的评论建议将state引入一个本应是无状态的类。@EdwardAung它没有说任何地方
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: ");
    }
}
}