2014-10-09 85 views
1

我對Java完全陌生,而且卡住了。我必須製作遊戲「猜數字」。我能夠完成大部分工作,但是現在我不知道如何處理用戶輸入,如果它的字符串。 我希望能夠告訴用戶輸入字符串時輸入不正確,並重復詢問輸入。這將是巨大的,如果有人可以幫助我在這裏:)
這裏是我的代碼: Java - 字符串輸入異常處理

import java.util.Scanner; 
import java.util.Random; 

public class SWENGB_HW_2 { 

public static void main(String[] args) { 

    System.out.println("Welcome to the guess the number game!\n"); 
    System.out.println("Please specify the configuration of the game:\n"); 

    Scanner input = new Scanner(System.in); 

    System.out.println("Range start number (inclusively):"); 
    int startRange; 
    startRange = input.nextInt(); 

    System.out.println("Range end (inclusively):"); 
    int endRange; 
    endRange = input.nextInt(); 

    System.out.println("Maximum number of attemps:"); 
    int maxAttemp; 
    maxAttemp = input.nextInt(); 

    System.out.println("Your Task is to guess the random number between " 
      + startRange + " and " + endRange); 

    Random randGenerator = new Random(); 
    int randNumber = randGenerator.nextInt((endRange - startRange) + 1) 
      + startRange; 
    int numberOfTries = 0; 

    System.out 
      .println("You may exit the game by typing; exit - you may now start to guess:"); 
    String exit; 
    exit = input.nextLine(); 


    for (numberOfTries = 0; numberOfTries <= maxAttemp - 1; numberOfTries++) { 

     int guess; 
     guess = input.nextInt(); 



     if (guess == randNumber) { 
      System.out.println("Congratz - you have made it!!"); 
      System.out.println("Goodbye"); 
     } else if (guess > randNumber) { 
      System.out.println("The number is smaller"); 
     } else if (guess < randNumber) { 
      System.out.println("The number is higher"); 
     } 

    } 
    if (numberOfTries >= maxAttemp) { 
     System.out.println("You reached the max Number of attempts :-/"); 
    } 

} 
} 
+0

可能重複http://stackoverflow.com/questions/25940276/understanding-try-catch-and-error -handling) – StackFlowed 2014-10-09 13:14:26

+0

打印'Goodbye'後可能要添加'break;'以退出for-loop並終止程序。 – aioobe 2014-10-09 13:31:03

回答

3

您可以創建一個看起來像這樣一個工具方法:

public static int nextValidInt(Scanner s) { 
    while (!s.hasNextInt()) 
     System.out.println(s.next() + " is not a valid number. Try again:"); 
    return s.nextInt(); 
} 

,然後,而不是

startRange = input.nextInt() 

你做

startRange = nextValidInt(input); 

如果要對付"exit"替代方案,我建議是這樣的:

public static int getInt(Scanner s) throws EOFException { 
    while (true) { 
     if (s.hasNextInt()) 
      return s.nextInt(); 
     String next = s.next(); 
     if (next.equals("exit")) 
      throw new EOFException(); 
     System.out.println(next + " is not a valid number. Try again:"); 
    } 
} 

,然後在

try { 
     ... 
     ... 
     ... 
    } catch (EOFException e) { 
     // User typed "exit" 
     System.out.println("Bye!"); 
    } 
} // End of main. 

順便說一句,其餘包住整個程序你的代碼看起來不錯。我試過了,它的功能就像是一種魅力:-)

+0

Thx那很好聽:) – chritschl 2014-10-11 09:56:54

+0

我還有一個問題。 「翹曲整個程序」是什麼意思?代碼的哪些部分? – chritschl 2014-10-12 18:10:11

+1

把'try {'作爲main(String [] args)之後的第一件事情{{和}} catch(EOFException e){System.out.println(「Bye」); }'在'main'的關閉'}'之前。通過在main中的任何位置拋出'EOFException',你最終會在'catch'塊中直接終止。 – aioobe 2014-10-12 18:11:51

1

在嘗試閱讀之前,您可以檢查掃描儀是否有int。你可以通過調用hasNextInt()喜歡的東西

while (input.hasNext() && !input.hasNextInt()) { 
    System.out.printf("Please enter an int, %s is not an int%n", input.next()); 
} 
int startRange = input.nextInt(); 
[理解嘗試與捕獲和錯誤處理(的