Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class CalculatorClient {
  4. public static void main(String args []){
  5. boolean exit = false;
  6. double number, number2;
  7. String operation, input;
  8.  
  9. Scanner in = new Scanner(System.in);
  10. System.out.println("ENTER INITIAL NUMBER:");
  11. number = in.nextDouble();
  12. in.nextLine(); // skip \n from initial input (nextDouble method would cause the later inputs to be skipped)
  13. System.out.print(number);
  14. Calculator calc = new Calculator(number);
  15.  
  16. while(exit!=true){
  17. input = in.nextLine();
  18. operation = input.substring(0, 1);
  19.  
  20. try{
  21. number2 = Double.parseDouble(input.substring(1, input.length()));
  22. }
  23. catch(Exception ex) {
  24. number2 = 0; //set a default number2 to avoid errors
  25. if(input.toLowerCase().equals("c")) {
  26. operation = "C";
  27. }
  28. else if (input.toLowerCase().equals("exit")){
  29. operation = "exit";
  30. }
  31. else {
  32. System.out.print("\nUnknown operation...\n" + number);
  33. continue; //skip while loop iteration
  34. }
  35. }
  36.  
  37. switch(operation) {
  38. case "+":
  39. System.out.print("=\n");
  40. System.out.print(calc.add(number2));
  41. break;
  42. case "-":
  43. System.out.print(calc.subtract(number2));
  44. break;
  45. case "/":
  46. System.out.print(calc.divide(number2));
  47. break;
  48. case "*":
  49. System.out.print(calc.multiply(number2));
  50. break;
  51. case "C":
  52. calc.clear();
  53. System.out.print("0");
  54. break;
  55. case "exit":
  56. System.out.println("EXIT");
  57. exit = true;
  58. break;
  59. default:
  60. System.out.println("Unknown operation...\n" + number);
  61. break;
  62.  
  63. }
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement