Advertisement
ew0345

Java Calculator

Nov 20th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Calculator {
  4.   public static void main(String[] args) {
  5.     try(Scanner a = new Scanner(System.in)) {
  6.       //Variables
  7.       String e = "";   //Store input text for math type
  8.       int b = 1;      //Store input for number 1
  9.       int c = 1;      //Store input for number 2
  10.      
  11.       //Get input for math type
  12.       System.out.print("Type: ");
  13.       e = a.nextLine();
  14.      
  15.       //Get input for number 1
  16.       System.out.print("Num1: ");
  17.       b = a.nextInt();
  18.      
  19.       //Get input for number 2
  20.       System.out.print("Num2: ");
  21.       c = a.nextInt();
  22.      
  23.       int[] d = {     //Do math
  24.         b+c,          //Add
  25.         b-c,          //Subtract
  26.         b*c,          //Multiply
  27.         b/c           //Divide
  28.       };
  29.      
  30.       //Decide what to do with input
  31.       switch(e.toLowerCase()) {
  32.       //Addition
  33.         case "1":
  34.         case "a":
  35.         case "add":
  36.         case "addition":
  37.         case "+":
  38.           System.out.println("\nAddition: "+d[0]);
  39.           break;
  40.       //Subtraction
  41.         case "2":
  42.         case "s":
  43.         case "sub":
  44.         case "subtract":
  45.         case "subtraction":
  46.         case "-":
  47.           System.out.println("\nSubtraction: "+d[1]);
  48.           break;
  49.       //Mutliplication
  50.         case "3":
  51.         case "m":
  52.         case "multi":
  53.         case "multiply":
  54.         case "multiplication":
  55.         case "*":
  56.         case "x":
  57.           System.out.println("\nMutliplication: "+d[2]);
  58.           break;
  59.       //Division
  60.         case "4":
  61.         case "d":
  62.         case "div":
  63.         case "divide":
  64.         case "division":
  65.         case "/":
  66.           System.out.println("\nDivision: "+d[3]);
  67.           break;
  68.       //All
  69.         case "5":
  70.         case "all":
  71.           System.out.println("\nAddition: "+d[0]
  72.             +"\nSubtraction: "+d[1]
  73.             +"\nMultiplication: "+d[2]
  74.             +"\nDivision: "+d[3]);
  75.           break;
  76.         default:
  77.           System.out.println("Invalid Type."
  78.             +"\nValid Types: "
  79.             +"\n1, a, add, addition, +"
  80.             +"\n2, s, sub, subtract, subtraction, -"
  81.             +"\n3, m, multi, multiply, multiplication, *"
  82.             +"\n4, d, div, divide, division, /"
  83.             +"\n5, all");
  84.           main(args);
  85.           break;
  86.       }
  87.     } catch (Exception e) {
  88.       e.printStackTrace();
  89.     }
  90.   }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement