Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class calculator {
- public static void main(String[] args) {
- int choice = 0;
- Scanner scan = new Scanner(System.in);
- while (choice != 5) {
- menu();
- choice = scan.nextInt();
- switch (choice) {
- case 1:
- add();
- break;
- case 2:
- sub();
- break;
- case 3:
- mult();
- break;
- case 4:
- div();
- break;
- case 5:
- quit();
- break;
- default:
- System.out.println("Not a valid choice. Please try again");
- menu();
- }
- }
- }
- public static void menu() {
- System.out.println("What do you want to do?");
- System.out.println("1. Add");
- System.out.println("2. Subtract");
- System.out.println("3. Multiply");
- System.out.println("4. Divide");
- System.out.println("5. Quit");
- System.out.println("Enter your choice:");
- }
- public static void add() {
- Scanner scan = new Scanner(System.in);
- double sum = 0;
- System.out.println("Enter number 1:");
- double a = scan.nextDouble();
- System.out.println("Enter number 2:");
- double b = scan.nextDouble();
- sum = a + b;
- System.out.println("Result: " + sum);
- }
- public static void sub() {
- Scanner scan = new Scanner(System.in);
- double sum = 0;
- System.out.println("Enter number 1:");
- double a = scan.nextDouble();
- System.out.println("Enter number 2:");
- double b = scan.nextDouble();
- sum = a - b;
- System.out.println("Result: " + sum);
- }
- public static void mult() {
- Scanner scan = new Scanner(System.in);
- double sum = 0;
- System.out.println("Enter number 1:");
- double a = scan.nextDouble();
- System.out.println("Enter number 2:");
- double b = scan.nextDouble();
- sum = a * b;
- System.out.println("Result: " + sum);
- }
- public static void div() {
- Scanner scan = new Scanner(System.in);
- double sum = 0;
- System.out.println("Enter number 1:");
- double a = scan.nextDouble();
- System.out.println("Enter number 2:");
- double b = scan.nextDouble();
- sum = a / b;
- System.out.println("Result: " + sum);
- }
- public static void quit() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement