Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Versi Lengkap : https://github.com/HouariZegai/Calculator
- private double calc(double x, String input, char opt) {
- inText.setFont(inText.getFont().deriveFont(Font.BOLD));
- double y = Double.parseDouble(input);
- int ans = 0;
- if (opt == '+') {
- return x + y;
- } else if (opt == '-') {
- return x - y;
- } else if (opt == '*') {
- return x * y;
- } else if (opt == '/') {
- if(y==0) { // ketika pembaginya bernilai 0
- int X = (int) x; //mencoba try-catch
- try {
- ans = (X/0);
- } catch (ArithmeticException e) {
- System.out.println(e);
- } catch (Exception e) {
- System.out.println("Exception occurred");
- }
- }else return x/y;
- } else if (opt == '%') {
- return x % y;
- } else if (opt == '^') {
- return Math.pow(x, y);
- }
- inText.setFont(inText.getFont().deriveFont(Font.PLAIN));
- return y;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement