Home > Enterprise >  Calculator Java
Calculator Java

Time:10-30

I wrote very simple calculator in Java. What other functionalities can i add and how can i improve this code? Thanks for every idea. Here's my code:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int a, b;
    System.out.print("Enter a value a = ");
    a = scanner.nextInt();
    System.out.print("Enter a value b = ");
    b = scanner.nextInt();

    int operation;
    System.out.println("Operations: \n 1 - Addition \n 2 - Subtraction \n 3 - Multiplication \n 4 - Division");
    System.out.println("Enter an operation = ");

    switch (operation = scanner.nextInt()) {
    case 1:
        System.out.println("Addition result: "   (a   b));
        break;
    case 2:
        System.out.println("Subtraction result: "   (a - b));
        break;
    case 3:
        System.out.println("Multiplication result: "   (a * b));
        break;
    case 4:
        if ((a % b) == 0) {
            System.out.println("Division result: "   ((float) a / (float) b));
        } else {
            System.out.println(
                    "Division result: "   ((float) a / (float) b)   ", The rest of the division: "   (a % b));
        }
        break;
    default:
        System.out.println("This operation doesnt't exist!");
    }
    scanner.close();
}

}

CodePudding user response:

Your code is looking nice but if you have an idea of methods in java , try using them in your code. Also add some trigonometric functions like sin , cos , tan by using Math class in java

CodePudding user response:

Your code is brilliant and neat. Good job. Try adding functions for finding the average of some numbers, square root, cube root. Also if you can add a function to find percentage % then that would be great. :)

  • Related