首页 > 解决方案 > 如何让扫描仪输入不触发第三种方法?

问题描述

我正在处理一项任务,并且大部分时间都完成了,但是我对最后一种方法有疑问。我正在尝试编写一个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”

标签: java

解决方案


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

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


推荐阅读