Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class main {
- static double accumulator = 0;
- public static void main(String [] args)
- {
- boolean loop = true;
- do{
- try
- {
- System.out.println("To start please select an operation");
- System.out.println("Press + for addition");
- System.out.println("Press - for subtraction");
- System.out.println("Press * for multiplication");
- System.out.println("Press / for division");
- System.out.println("Press R to reset the accumulator to 0");
- System.out.println("Press P to power off the calculator");
- Scanner keyboard = new Scanner(System.in);
- String input = keyboard.next();
- unknownOperator(input);
- switch(input){
- case "+":
- System.out.println("Type in one number:");
- String x = keyboard.next();
- NumberFormatException(x);
- double input1 = Double.parseDouble(x);
- accumulator += input1;
- System.out.println("The sum is: " + accumulator);
- break;
- case "-":
- System.out.println("Type in one number:");
- String x1 = keyboard.next();
- NumberFormatException(x1);
- double input2 = Double.parseDouble(x1);
- accumulator -= input2;
- System.out.println("The difference is: " + accumulator);
- break;
- case "*":
- System.out.println("Type in one number:");
- String x2 = keyboard.next();
- NumberFormatException(x2);
- double input3 = Double.parseDouble(x2);
- accumulator -= input3;
- System.out.println("The product is: " + accumulator);
- break;
- case "/":
- System.out.println("Type in one number:");
- String x3 = keyboard.next();
- NumberFormatException(x3);
- double input4 = Double.parseDouble(x3);
- accumulator -= input4;
- divideByZero(input4);
- System.out.println("The product is: " + accumulator);
- break;
- case "R":
- accumulator = 0;
- System.out.println("Accumulator has been set to 0");
- break;
- case "P":
- System.out.println("Shutting off calculator");
- break;
- }
- }
- catch(DivideByZeroException e)
- {
- System.out.println(e.getMessage());
- }
- catch(UnknownOperatorException e)
- {
- System.out.println(e.getMessage());
- }
- catch(NumberFormatException e)
- {
- System.out.println(e.getMessage());
- }
- }
- while(loop == true);
- }
- public static void divideByZero(double valueTwo) throws DivideByZeroException
- {
- if(valueTwo == 0)
- {
- throw new DivideByZeroException("You cannot divide by zero, try again.");
- }
- }
- public static void unknownOperator(String sign) throws UnknownOperatorException
- {
- if(sign.equals("+") || sign.equals("-") || sign.equals("*") || sign.equals("/") || sign.equals("R") || sign.equals("P"))
- {
- } else {
- throw new UnknownOperatorException("That is an unrecognized symbol, try again.");
- }
- System.out.println("nice");
- }
- public static void NumberFormatException(String string) {
- if (string.equals("R")) {
- accumulator = 0;
- System.out.println("Accumulator has been set to 0");
- } else if (string.equals("P")) {
- System.out.println("Shutting off calculator");
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement