2013-03-23 68 views
0

在此Java程序中,用户应猜测1到100之间的数字,然后如果按S它会显示尝试的摘要。问题是我正在输入字符串并将其转换为数字,以便将其与范围进行比较,但是我还需要能够将该字符串用作菜单输入。 更新如何在用户猜测正确后让程序回到菜单选项。用户赢得打完,我想为显示其可以通过使用S-尝试将字符串解析为int时出错

这里以其他方式访问的总结报告中的问题,是我的代码

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(); 
    } 
    } 
} 
+1

在此处发布您的代码。 – Simulant 2013-03-23 20:45:09

+1

你说的不是问题,而是一个计划。为什么你不能将它用作菜单中的输入并将其转换为整数来比较范围? – Michael 2013-03-23 20:47:11

+0

OP想知道如何检查'command'是否是一个数字。键入S或Q将会抛出一个'NumberFormatException',并且当前的代码为 – 2013-03-23 20:47:55

回答

0

您应该首先检查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/
  • 用户输入q/Q
  • 用户进入非有效值(非数字)
  • 用户进入非有效数(小于0或大于100)
  • 用户ENT一个有效的号码
+0

这工作。非常感谢 – 2013-03-23 22:42:03

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"); 
} 
0

的Integer.parseInt(命令)会给你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){ 
+0

我仍然会包装Integer。parseInt在try-catch块中,以防止用户部分可能出现的愚蠢;) – MadProgrammer 2013-03-23 20:53:40

+0

是的,总是建议采取预防措施,因为用户可能会疯狂并输入任何内容,即使预期为int值也是如此。 :-) – 2013-03-23 20:55:23

+0

我试图修改代码,因为你建议,但即时通讯仍然得到相同的numberformat例外 – 2013-03-23 22:09:44

0

你试图进入String转换为int你检查它的转义序列(S或Q)前。

尝试重新安排您的if声明以检查S和Q,然后尝试将该值转换为int

我会还建议您缠绕Integer.parseInt调用(它的后续,依赖代码)在try-catch块,这样你就可以提供错误的语句给用户,如果他们中的任何类型,它是不是一个int