2013-03-23 67 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