Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class CalculatorClient {
- public static void main(String args []){
- boolean exit = false;
- double number, number2;
- String operation, input;
- Scanner in = new Scanner(System.in);
- System.out.println("ENTER INITIAL NUMBER:");
- number = in.nextDouble();
- in.nextLine(); // skip \n from initial input (nextDouble method would cause the later inputs to be skipped)
- System.out.print(number);
- Calculator calc = new Calculator(number);
- while(exit!=true){
- input = in.nextLine();
- operation = input.substring(0, 1);
- try{
- number2 = Double.parseDouble(input.substring(1, input.length()));
- }
- catch(Exception ex) {
- number2 = 0; //set a default number2 to avoid errors
- if(input.toLowerCase().equals("c")) {
- operation = "C";
- }
- else if (input.toLowerCase().equals("exit")){
- operation = "exit";
- }
- else {
- System.out.print("\nUnknown operation...\n" + number);
- continue; //skip while loop iteration
- }
- }
- switch(operation) {
- case "+":
- System.out.print("=\n");
- System.out.print(calc.add(number2));
- break;
- case "-":
- System.out.print(calc.subtract(number2));
- break;
- case "/":
- System.out.print(calc.divide(number2));
- break;
- case "*":
- System.out.print(calc.multiply(number2));
- break;
- case "C":
- calc.clear();
- System.out.print("0");
- break;
- case "exit":
- System.out.println("EXIT");
- exit = true;
- break;
- default:
- System.out.println("Unknown operation...\n" + number);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement