1

在这个 Java 程序中,用户应该猜一个从 1 到 100 的数字,然后如果你按下S它,就会显示一个尝试的摘要。问题是我正在获取输入字符串并将其转换为数字,以便可以将其与范围进行比较,但是我还需要能够将该字符串用作菜单输入。更新用户正确猜测后,如何使程序返回菜单选项。因此,在用户获胜后,我希望问题显示可以使用 S 访问的摘要报告

这是我的代码

public class GuessingGame {
  public static void main(String[] args) {


    // Display list of commands
                System.out.println("*************************");
                System.out.println("The Guessing Game-inator");
                System.out.println("*************************");  
                System.out.println("Your opponent has guessed a number!");
                System.out.println("Enter a NUMBER at the prompt to guess.");
                System.out.println("Enter [S] at the prompt to display the summary report.");
                System.out.println("Enter [Q] at the prompt to Quit.");
                System.out.print("> ");


    // Read and execute commands
    while (true) {

      // Prompt user to enter a command
      SimpleIO.prompt("Enter command (NUMBER, S, or Q): ");
      String command = SimpleIO.readLine().trim();

      // Determine whether command is "E", "S", "Q", or
      // illegal; execute command if legal.
      int tries = 0;
      int round = 0;
      int randomInt = 0;
      int number = Integer.parseInt(command);
      if (number >= 0 && number <= 100) {
        if(randomInt == number){

                System.out.println("Congratulations! You have guessed correctly." +
                                " Summary below");
                round++;
        }
        else if(randomInt < number)
        {
                System.out.println("your guess is TOO HIGH. Guess again or enter Q to Quit");
                tries++;
        }      
        else if(randomInt > number){
                System.out.println("your guess is TOO LOW. Guess again or enter Q to Quit");
                tries++;
        }

      } else if (command.equalsIgnoreCase("s")) {
         // System.out.println("Round        Guesses");
         // System.out.println("-------------------------");
        //  System.out.println(round + "" + tries);



      } else if (command.equalsIgnoreCase("q")) {
        // Command is "q". Terminate program.
        return;

      } else {
        // Command is illegal. Display error message.
        System.out.println("Command was not recognized; " +
                           "please enter only E, S, or q.");
      }

      System.out.println();
    }
  }
}
4

4 回答 4

1

要检查字符串是否为整数,只需尝试将其解析为整数,如果抛出异常,则它不是整数。

看:

http://bytes.com/topic/java/answers/541928-check-if-input-integer

String input = ....
try {
    int x = Integer.parseInt(input);
    System.out.println(x);
}
catch(NumberFormatException nFE) {
    System.out.println("Not an Integer");
}
于 2013-03-23T20:50:37.763 回答
1

您应该首先检查 S/Q 值,然后将字符串解析为整数。如果您捕获 NumberFormatException(由 引发Integer.parseInt()),您可以确定输入是否为有效值。我会做这样的事情:

if ("s".equalsIgnoreCase(command)) {
    // Print summary
} else if ("q".equalsIgnoreCase(command)) {
    // Command is "q". Terminate program.
    return;
} else {
    try {
        Integer number = Integer.parseInt(command);
        if(number < 0 || number > 100){
            System.out.println("Please provide a value between 0 and 100");
        } else if(randomInt == number){
            System.out.println("Congratulations! You have guessed correctly." +
                        " Summary below");
            round++;
        } else if(randomInt < number) {
            System.out.println("your guess is TOO HIGH. Guess again or enter Q to Quit");
                 tries++;
        } else if(randomInt > number) {
            System.out.println("your guess is TOO LOW. Guess again or enter Q to Quit");
            tries++;
        }
    } catch (NumberFormatException nfe) {
        // Command is illegal. Display error message.
        System.out.println("Command was not recognized; " +
                       "please enter only a number, S, or q.");
    }
}

使用此算法(我确信它可以优化),您可以处理以下情况:

  • 用户输入 s/S
  • 用户输入q/Q
  • 用户输入无效值(不是数字)
  • 用户输入无效数字(小于 0 或大于 100)
  • 用户输入有效数字
于 2013-03-23T21:08:12.070 回答
0

如果字符串无效,Integer.parseInt(command) 会给你 NumberFormatException。如果用户输入无法解析为 int 值的“S”或“E”,则可能在您的代码中。

我已经修改了你的代码。检查此代码:

 while (true) {

          // Prompt user to enter a command
          SimpleIO.prompt("Enter command (NUMBER, S, or Q): ");
          String command = SimpleIO.readLine().trim();

          // Determine whether command is "E", "S", "Q", or
          // illegal; execute command if legal.
          int tries = 0;
          int round = 0;
          int randomInt = 0;
          if(!command.equals("S") && !command.equals("E")) {
            // Only then parse the command to string  

          int number = Integer.parseInt(command);
          if (number >= 0 && number <= 100) {
            if(randomInt == number){
于 2013-03-23T20:51:09.527 回答
0

在检查它是否为转义序列(S 或 Q)之前,您正在尝试将传入的转换String为 an 。int

尝试重新排列您的if语句以首先检查 S 和 Q,然后尝试将值转换为int.

我还建议您将Integer.parseInt调用(它是后续的依赖代码)包装在一个try-catch块中,这样您就可以向用户提供一个错误语句,如果他们输入任何不是 int 的内容

于 2013-03-23T20:51:10.953 回答