2014-09-10 195 views
-1

我做了這個Java代碼來輸入一個數字:防止輸入錯誤

public static void main(String[] args) 
{ 
    int temp; 

    do{ 

     Scanner scan = new Scanner(in); 
     out.print("enter number "); 
     temp = scan.nextInt(); 




     if(temp >= 5 && temp <= 40){ 
      int x = (temp-1)*2 +1; 
      int y = x/2; 
      int z = 1; 
      for(int i=0; i<temp-1; i++) 
      { 
       for(int j=0; j<=y; j++) 
       { 
        out.print(" "); 
       } 
       for(int k = 0; k<z; k++) 
       { 
        out.print("|"); 
       } 
       out.println(); 
       y--; 
       z+=2; 
      } 

      for(int c = 0; c < 1 + temp/10; c++) { 
       for (int i = 0; i <= x/2; i++) 
       { 
        System.out.print(" "); 
       } 
       System.out.println("|"); 
      } 
     }else{ 
      out.print("enter a number between 5 and 40"); 
     } 
    }while(temp != 0); 
} 

}

然而,這要是我,例如輸入字母或無效字符返回一個錯誤。我想知道如何在不使程序崩潰的情況下顯示錯誤消息,然後再次詢問問題,直到輸入正確爲止?

回答

1

import java.io. *;

import java.util。*;

公共類主要

{

public static void main(String[] args) 
{ 

    int temp=0; 
    boolean error=false; 
    do{ 
     error=false; 
     try 
     { 
      Scanner scan = new Scanner(System.in); 
      System.out.print("enter number "); 
      temp = scan.nextInt(); 
      if(temp==0) 
       break; 



      if(temp >= 5 && temp <= 40) 
      { 
       int x = (temp-1)*2 +1; 
       int y = x/2; 
       int z = 1; 
       for(int i=0; i<temp-1; i++) 
       { 
        for(int j=0; j<=y; j++) 
        { 
         System.out.print(" "); 
        } 
        for(int k = 0; k<z; k++) 
        { 
         System.out.print("|"); 
        } 
        System.out.println(); 
        y--; 
        z+=2; 
       } 

       for(int c = 0; c < 1 + temp/10; c++) 
       { 
        for (int i = 0; i <= x/2; i++) 
        { 
         System.out.print(" "); 
        } 
        System.out.println("|"); 
       } 
      } 
      else 
      { 
       System.out.println("enter a number between 5 and 40"); 
      } 
     }catch(Exception e) 
     { 
      System.out.println("Enter a valid number..try again"); 
      error=true; 
     } 
    }while(temp != 0 || error); 
} 

}

當錯誤或異常的scan.nextInt(即發生)的異常被拋出,當你還沒有抓到異常的JVM停止執行程序。

因此,總是編寫可以在try {}塊內引發異常的語句,並立即跟隨catch(Exception e){}塊來捕獲異常。如果沒有發生異常,catch塊將不會執行。如果在try {}塊內發生任何錯誤:控件跳轉到catch塊並且它被執行,並且try {}中的所有其他語句(在錯誤行之後)被忽略。

嘗試 { .. 錯誤 .. //跳過 .. } 趕上(例外五){ ... ... //處理異常 } // 來控制這裏執行catch塊

+0

你的答案需要一些解釋 – 2014-09-10 22:51:47

+0

據我所知,在主要方法中拋出異常並不是很好的做法 – 2014-09-10 22:52:29

+0

@KickButtowski ..他沒有拋出異常。他正在趕上它 – RKodakandla 2014-09-10 22:54:57

0

以字符串形式讀取用戶的值,然後嘗試將其解析爲try {} catch {}塊中的數字,如果引發異常解析,請告訴用戶只有數字是可接受的。如果沒有繼續處理他們給你的號碼。

+0

我更直觀;你能告訴我一個例子或使用我的代碼嗎?謝謝 – 2014-09-10 22:34:54

+1

@Johnny_b:Google是你的朋友。有成千上萬的例子。只需查找try/catch字符串,或類似的東西。 – 2014-09-10 22:38:25

+0

把「Shouvik Roy」的代碼放在一個循環中,直到你得到一個有效的輸入並且你很好。 – Dave 2014-09-10 22:42:43

0

從@Shouvik羅伊的回答後擴展..

import java.util.Scanner; 

public class Bingo { 

    public static void main(String[] args){ 
     boolean isInt = false; 
     int temp; 
     System.out.print("enter a number "); 
     do{ 
      Scanner scan = new Scanner(System.in); 

      try 
      { 
       temp = scan.nextInt(); 
       isInt = true; 
      } 
      catch(Exception e) 
      { 
       System.out.println("Couldn't read number. Reason - "+e.getMessage()); 
       System.out.println("Enter a valid number "); 
      } 

     }while(!isInt); 

    } 
} 

------------------編輯-------------------

boolean isInt = false; 
System.out.print("enter a number "); 


do{ 
    try 
    { 
     Scanner scan = new Scanner(in); 
     temp = scan.nextInt(); 
     isInt = true; 
    } 
    catch(Exception e) 
    { 
     System.out.println("Couldn't read number. Reason - "+e.getMessage()); 
     System.out.println("Enter a valid number "); 
    } 
}while(!isInt) 
+0

如果你的掃描儀遇到問題,你將如何處理它?你以爲沒有解釋給出答案是正確的和有幫助的? – 2014-09-10 22:54:03

+0

@KickButtowski在控制檯中添加了打印錯誤信息的行.. – RKodakandla 2014-09-10 22:57:19

+0

謝謝,但是你能否把它放到我的代碼中去看看你到底是什麼意思?我有一些if和while語句,所以我不知道你的方法是否與我的代碼 – 2014-09-10 23:09:04

0

Scanner有一個方法來檢查下一個標記是否是整數:hasNextInt

你可以這樣做:

package com.sandbox; 

import java.util.Scanner; 

public class Sandbox { 

    public static void main(String[] args) { 
     Scanner scanner = new Scanner(System.in); 

     System.out.println("Enter an integer:"); 
     while (!scanner.hasNextInt()) { 
      System.err.println("Wrong! Enter an integer"); 
      scanner.next(); 
     } 
     System.out.println("Your integer was: " + scanner.nextInt()); 
    } 


} 

此擔任你想要在Windows控制檯上。我有點擔心它會在Linux/Mac上錯誤地工作。任何人都在爲我測試它嗎?如果這樣做,我最喜歡它,因爲沒有嘗試/抓住。

+0

只是一個問題,如果你的掃描儀碰到問題你將如何處理它? – 2014-09-10 22:56:50

+0

@KickButtowski它會遇到什麼問題? – 2014-09-10 22:57:08

+0

先生,我相信你很聰明,已經知道它已經 – 2014-09-10 22:58:06