Advertisement
Lycaenion

calculator

Sep 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class calculator {
  4.  
  5.     public static void main(String[] args) {
  6.         int choice = 0;
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.  
  10.         while (choice != 5) {
  11.  
  12.             menu();
  13.             choice = scan.nextInt();
  14.  
  15.             switch (choice) {
  16.                 case 1:
  17.                     add();
  18.                     break;
  19.                 case 2:
  20.                     sub();
  21.                     break;
  22.                 case 3:
  23.                     mult();
  24.                     break;
  25.                 case 4:
  26.                     div();
  27.                     break;
  28.                 case 5:
  29.                     quit();
  30.                     break;
  31.                 default:
  32.                     System.out.println("Not a valid choice. Please try again");
  33.                     menu();
  34.  
  35.             }
  36.         }
  37.  
  38.     }
  39.  
  40.  
  41.  
  42.     public static void menu() {
  43.         System.out.println("What do you want to do?");
  44.         System.out.println("1. Add");
  45.         System.out.println("2. Subtract");
  46.         System.out.println("3. Multiply");
  47.         System.out.println("4. Divide");
  48.         System.out.println("5. Quit");
  49.         System.out.println("Enter your choice:");
  50.     }
  51.  
  52.     public static void add() {
  53.         Scanner scan = new Scanner(System.in);
  54.         double sum = 0;
  55.  
  56.         System.out.println("Enter number 1:");
  57.         double a = scan.nextDouble();
  58.         System.out.println("Enter number 2:");
  59.         double b = scan.nextDouble();
  60.  
  61.         sum = a + b;
  62.  
  63.         System.out.println("Result: " + sum);
  64.     }
  65.  
  66.     public static void sub() {
  67.         Scanner scan = new Scanner(System.in);
  68.         double sum = 0;
  69.  
  70.         System.out.println("Enter number 1:");
  71.         double a = scan.nextDouble();
  72.         System.out.println("Enter number 2:");
  73.         double b = scan.nextDouble();
  74.  
  75.         sum = a - b;
  76.  
  77.         System.out.println("Result: " + sum);
  78.     }
  79.  
  80.     public static void mult() {
  81.         Scanner scan = new Scanner(System.in);
  82.         double sum = 0;
  83.  
  84.         System.out.println("Enter number 1:");
  85.         double a = scan.nextDouble();
  86.         System.out.println("Enter number 2:");
  87.         double b = scan.nextDouble();
  88.  
  89.         sum = a * b;
  90.  
  91.         System.out.println("Result: " + sum);
  92.     }
  93.  
  94.     public static void div() {
  95.         Scanner scan = new Scanner(System.in);
  96.         double sum = 0;
  97.  
  98.         System.out.println("Enter number 1:");
  99.         double a = scan.nextDouble();
  100.         System.out.println("Enter number 2:");
  101.         double b = scan.nextDouble();
  102.  
  103.         sum = a / b;
  104.  
  105.         System.out.println("Result: " + sum);
  106.     }
  107.  
  108.     public static void quit() {
  109.  
  110.     }
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement