2013-10-17 19 views
0

我最初試圖讓我的throw語句工作沒有一個try catch和userInput = input.nextInt();行工作正常。但是,當我嘗試添加try..catch時,它不喜歡我的輸入說它無法解析。我不認爲我的try..catch是正確的,但我計劃在解決這個問題後,我可以得到這個輸入被識別,但我會很感激任何反饋,你也看到了這些。「輸入無法解析」當添加一個try..catch

感謝

import java.util.Scanner; 

    public class Program6 
    { 
     public static void main(String[] args) 
     { 
      final int NUMBER_HIGH_LIMIT = 100; 
      final int NUMBER_LOW_LIMIT = 10; 
      int userInput; 

      try 
      { 
       System.out.print("Enter a number between 10 and 100: "); 
       userInput = input.nextInt();//Says input cannot be resolved 

       Verify v = new Verify(NUMBER_HIGH_LIMIT, NUMBER_LOW_LIMIT); 
      } 
      catch(NumberHighException exception) 
      { 
       userInput = 0; 
      } 
      catch(NumberLowException exception) 
      { 
       userInput = 0; 
      } 
     } 
    } 
+0

變量需要聲明之前,你可以在Java中使用它們。 –

+1

可能是他在添加try/catch時意外刪除了聲明行。 –

回答

4

你需要創建一個名爲input掃描儀:

public class Program6 { 

    public static void main(String[] args) { 
    final int NUMBER_HIGH_LIMIT = 100; 
    final int NUMBER_LOW_LIMIT = 10; 
    int userInput; 

    try { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter a number between 10 and 100: "); 
     userInput = input.nextInt();//Says input cannot be resolved 

     Verify v = new Verify(NUMBER_HIGH_LIMIT, NUMBER_LOW_LIMIT); 

    } catch (NumberHighException exception) { 
     userInput = 0; 
    } catch (NumberLowException exception) { 
     userInput = 0; 
    } 
    } 
}