Advertisement
mbah_bejo

trycatchtraining

Dec 21st, 2020 (edited)
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. Versi Lengkap : https://github.com/HouariZegai/Calculator
  2.  
  3. private double calc(double x, String input, char opt) {
  4.         inText.setFont(inText.getFont().deriveFont(Font.BOLD));
  5.         double y = Double.parseDouble(input);
  6.         int ans = 0;
  7.         if (opt == '+') {
  8.             return x + y;
  9.         } else if (opt == '-') {
  10.             return x - y;
  11.         } else if (opt == '*') {
  12.             return x * y;
  13.         } else if (opt == '/') {
  14.            
  15.             if(y==0) { // ketika pembaginya bernilai 0
  16.                 int X = (int) x; //mencoba try-catch
  17.                 try {
  18.                     ans = (X/0);
  19.                 } catch (ArithmeticException e) {
  20.                     System.out.println(e);
  21.                 } catch (Exception e) {
  22.                     System.out.println("Exception occurred");
  23.                 }
  24.             }else return x/y;
  25.         } else if (opt == '%') {
  26.             return x % y;
  27.         } else if (opt == '^') {
  28.             return Math.pow(x, y);
  29.         }
  30.         inText.setFont(inText.getFont().deriveFont(Font.PLAIN));
  31.         return y;
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement