2017-09-01 64 views
0

我是新來的java,但我已經試驗了很多這個特定的程序,我打了一堵牆。該程序的目的是捕捉用戶是否輸入超出變量限制的值,並驗證輸入是否滿足我指定的範圍。驗證輸入和防止嘗試/趕上退出

  • 如果輸入超過變量限制而沒有while循環,程序將退出。
  • 如果輸入超出了while循環的變量限制,它將無限循環。

我用while循環和我的三個結果嘗試此:

  1. 用戶輸入一個程序員指定有效數字,一切都很好。
  2. 用戶輸入一個高於100的數字,並提示再次嘗試...
  3. 用戶超出變量限制,程序進入 無限循環。

如果我在catch塊中放置一個轉義,它的作用就好像不存在時一樣。我希望程序打印錯誤,但允許用戶重試一個值。

while(!success){ 
    try{ 
     sh = sc.nextShort(); 
     while(sh < 1 || sh > 100){ 
      System.out.print("You must enter a value between 1-100: "); 
      sh = sc.nextShort(); 
     } //close try 
     System.out.println("Great job!"); 
     success = true; //escape the while 
    }catch{Exception ex){ 
     System.out.println("ERROR: " + ex); 
     success = false; //causes infinite loop... I understand 
    } //close catch 
} //close while 
+0

我不知道我理解你的問題,但我懷疑你的問題是,第一'SH = sc.nextShort();'在try-block中。如果那是你所有的代碼,我作爲一個用戶不會知道我必須輸入一些東西,所以我會等待程序打印一些東西,程序會等待我的輸入 - 一個典型的死鎖。我懷疑發生錯誤時會發生同樣的情況,即它會打印錯誤,然後再次啓動外部循環的主體,該主體開始等待輸入數字。打印出錯後,一個簡單的「修復」就是打印「再試一次:」這樣的內容。 – Thomas

+0

[使用java.util.Scanner驗證輸入]的可能重複(https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom

+0

問題是,當引發異常時,它會無限循環錯誤消息。我通過在catch中創建一個新的「Try again:」提示符和掃描器來跟蹤下面的帖子。它很實用,但我仍然有很多學習要做,以便準確理解發生了什麼。 感謝您的建議! – kaygeee

回答

1

的問題是輸入 ,該掃描儀存儲整個輸入字符串並對其進行處理。 當你的輸入不是一個短值,而是一個字符串時,它會引發異常。您在catch中記錄異常,並在下一個循環中嘗試從同一個存儲的字符串中讀取一個短值,然後再次拋出異常.... - >無限循環。

如果創建在catch一個新的Scanner,它會再次從控制檯讀取:

while (!success) { 
     try { 
      sh = sc.nextShort(); 
      while (sh < 1 || sh > 100) { 
       System.out.print("You must enter a value between 1-100: "); 
       sh = sc.nextShort(); 
      } //close try 
      System.out.println("Great job!"); 
      success = true; //escape the while 
     } catch (Exception ex) { 
      System.out.println("ERROR: " + ex); 
      sc = new Scanner(System.in); 
     } //close catch 
    } 
+0

我測試了這個,它工作!我仍然不是很瞭解,但我已經標記爲正確,我會做更多的學習。謝謝! – kaygeee

0

如果我正確理解你的問題,你可以擺脫你的第二個while循環,並用if替換它。然後你可以打印出這個值必須是1-100,並拋出異常,讓你的程序通過catch語句並打印出你拋出的錯誤。

while(!success){ 
    try{ 
     sh = sc.nextShort(); 
     if(sh < 1 || sh > 100){ 
      System.out.print("You must enter a value between 1-100"); 
      throw Exception; 
     } //close try 
     System.out.println("Great job!"); 
     success = true; //escape the while 
    }catch{Exception ex){ 
     System.out.println("ERROR: " + ex); 
     success = false; //causes infinite loop... I understand 
    } //close catch 
} //close while 
1

據我瞭解你的問題,你面對異常的環路上傳遞一個更大的值(更大的東西像e.g「百億」)。

你有例外未來如下面的死循環:

ERROR: java.util.InputMismatchException: For input string: "10000000000" 
ERROR: java.util.InputMismatchException: For input string: "10000000000" 

但是你想程序打印一個例外線,然後再允許用戶輸入。

您可以做到這一點下面的讀取錯誤的輸入(字符串bad_input = sc.next();)的方式從你使用的掃描儀

while (!success) { 
        try { 
         sh = sc.nextShort(); 
         while (sh < 1 || sh > 100) { 
           System.out.print("You must enter a value between 1-100: "); 
           sh = sc.nextShort(); 
         } // close try 
         System.out.println("Great job!"); 
         success = true; // escape the while 
        } catch (Exception ex) { 
         System.out.println("ERROR: " + ex); 
         success = false; // causes infinite loop... I understand 
         String bad_input = sc.next();// your bad input to allow read that exception 
        } // close catch 
      } // close while 

這個問題的原因可以發現here

如果轉換成功,則掃描器執行匹配