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) {
- try(Scanner a = new Scanner(System.in)) {
- //Variables
- String e = ""; //Store input text for math type
- int b = 1; //Store input for number 1
- int c = 1; //Store input for number 2
- //Get input for math type
- System.out.print("Type: ");
- e = a.nextLine();
- //Get input for number 1
- System.out.print("Num1: ");
- b = a.nextInt();
- //Get input for number 2
- System.out.print("Num2: ");
- c = a.nextInt();
- int[] d = { //Do math
- b+c, //Add
- b-c, //Subtract
- b*c, //Multiply
- b/c //Divide
- };
- //Decide what to do with input
- switch(e.toLowerCase()) {
- //Addition
- case "1":
- case "a":
- case "add":
- case "addition":
- case "+":
- System.out.println("\nAddition: "+d[0]);
- break;
- //Subtraction
- case "2":
- case "s":
- case "sub":
- case "subtract":
- case "subtraction":
- case "-":
- System.out.println("\nSubtraction: "+d[1]);
- break;
- //Mutliplication
- case "3":
- case "m":
- case "multi":
- case "multiply":
- case "multiplication":
- case "*":
- case "x":
- System.out.println("\nMutliplication: "+d[2]);
- break;
- //Division
- case "4":
- case "d":
- case "div":
- case "divide":
- case "division":
- case "/":
- System.out.println("\nDivision: "+d[3]);
- break;
- //All
- case "5":
- case "all":
- System.out.println("\nAddition: "+d[0]
- +"\nSubtraction: "+d[1]
- +"\nMultiplication: "+d[2]
- +"\nDivision: "+d[3]);
- break;
- default:
- System.out.println("Invalid Type."
- +"\nValid Types: "
- +"\n1, a, add, addition, +"
- +"\n2, s, sub, subtract, subtraction, -"
- +"\n3, m, multi, multiply, multiplication, *"
- +"\n4, d, div, divide, division, /"
- +"\n5, all");
- main(args);
- break;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement