2017-09-12 30 views
0

我想創建一個程序來處理分割兩個整數時發生的3個可能的異常,要求用戶在觸發異常時糾正輸入。代碼僅在沒有觸發例外的情況下執行。下面的代碼有效,但我覺得它太不優化了。除了循環之外,沒有其他方法可以連續檢查異常嗎?在java中的異常處理優化

import javax.swing.JOptionPane; 

public class DivisionExceptions { 

public int divide(int num, int den) { 
    return num/den; 
} 

public static void main(String[] args) { 
    int num = 0, 
     den = 0, 
     a = 0, 
     b = 0, 
     c = 0; 

    DivisionExceptions div = new DivisionExceptions(); 

     while(a == 0) { 
      try { 
       num = Integer.parseInt(JOptionPane.showInputDialog("Introduce the first int")); 
       a++; 
      } 
      catch (NumberFormatException e) { 
       JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again."); 
      } 
     } 
    while (c == 0) {  
     b = 0; 
     while(b == 0) { 
      try { 
       den = Integer.parseInt(JOptionPane.showInputDialog("Introduce the second int")); 
       b++; 
      } 
      catch (NumberFormatException e) { 
       JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again."); 
      } 
     } 

     try { 
      JOptionPane.showMessageDialog(null, "Result of dividing: " + num + "/" + den + " is " + div.divide(num, den)); 
      c++; 
     } 
     catch (ArithmeticException e) { 
      JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again."); 
     } 
    } 
} 

}

+0

這很好,你做的任何優化都是完全不會察覺的。儘管你可能想要清理你的標籤間距。 –

+0

當有一個ArithmeticException時,他們只能回去爲'den「而不是'num'嘗試另一個值。這是意圖嗎? – jingx

+0

@jingx是的,這是意圖,因爲如果分母是0,沒有理由修改分子。 – Razonixx

回答

0

可以簡化爲:

public static void main(String[] args) { 
    int num = 0, den = 0; 

    DivisionExceptions div = new DivisionExceptions(); 

    while(true) { 
     try { 
      num = Integer.parseInt(JOptionPane.showInputDialog("Introduce the first int")); 
      break; 
     } catch (NumberFormatException e) { 
      JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again."); 
     } 
    } 

    while (true) { 
     try { 
      den = Integer.parseInt(JOptionPane.showInputDialog("Introduce the second int")); 
      JOptionPane.showMessageDialog(null, "Result of dividing: " + num + "/" + den + " is " + div.divide(num, den)); 
      break; 
     } catch (NumberFormatException | ArithmeticException e) { 
      JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again."); 
     } 
    } 
} 

+0

這就是我一直在尋找的,謝謝! – Razonixx

0

嗯,你的代碼可以使用一些重構。

public class DivisionExceptions { 

public int divide(int num, int den) { 
    return num/den; 
} 

public static void main(String[] args) { 
    int num = 0, 
      den = 0, 
      a = 0, 
      b = 0, 
      c = 0; 

    DivisionExceptions div = new DivisionExceptions(); 
    num = getNum(a, "Introduce the first int"); 
    den = getNum(b, "Introduce the second int"); 
    while (c == 0) { 
     try { 
      JOptionPane.showMessageDialog(null, "Result of dividing: " + num + "/" + den + " is " + div.divide(num, den)); 
      c++; 
     } catch (ArithmeticException e) { 
      JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again."); 
     } 
    } 
} 

private static int getNum(int loopParam, String message) { 
    int num = 0; 
    while (loopParam == 0) { 
     try { 
      num = Integer.parseInt(JOptionPane.showInputDialog(message)); 
      loopParam++; 
     } catch (NumberFormatException e) { 
      JOptionPane.showMessageDialog(null, "Error of type: " + e.getMessage() + ". Verify the information and try again."); 
     } 
    } 
    return num; 
} 
} 

我也讓自己從,而(C == 0)循環提取書房計算,因爲它總是計算相同的價值,但對於N次,所以你在這裏獲得一些optimilization。如果你可以提供更多關於你爲什麼預定義所有參數爲0的信息,那麼也許我可以在while(c == 0)循環中找到一些解決方案。如果你使用java 8,你也可以將while循環提取到另一個方法,並將一些函數作爲參數。