Advertisement
Guest User

poorly written calculator thingy

a guest
Jul 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Calculator {
  4. public static void main(String args[]) {
  5. double a, b;
  6. double answer=0;
  7. boolean cont;
  8. String operation;
  9. Scanner scanner = new Scanner(System.in);
  10. System.out.println("First Number: ");
  11. a = scanner.nextDouble();
  12. System.out.println("Operation: ");
  13. operation = scanner.next();
  14. System.out.println("Second Number: ");
  15. b = scanner.nextDouble();
  16.  
  17. switch (operation) {
  18. case "+":
  19. answer = a + b;
  20. break;
  21. case "-":
  22. answer = a - b;
  23. break;
  24. case "*":
  25. answer = a * b;
  26. break;
  27. case "/":
  28. answer = a / b;
  29. break;
  30. default:
  31. System.out.println("Operation cannot be performed");
  32. }
  33. System.out.println("Your answer is = " + answer + "\nContinue?");
  34.  
  35. if (scanner.next().equalsIgnoreCase("yes")) {
  36. cont = true;
  37. while (cont) {
  38. System.out.println("Operation: ");
  39. operation = scanner.next();
  40. System.out.println("Number: ");
  41. b = scanner.nextDouble();
  42. switch (operation) {
  43. case "+":
  44. answer = answer + b;
  45. break;
  46. case "-":
  47. answer = answer - b;
  48. break;
  49. case "*":
  50. answer = answer * b;
  51. break;
  52. case "/":
  53. answer = answer / b;
  54. break;
  55. default:
  56. System.out.println("Operation cannot be performed");
  57. }
  58. System.out.println("Your answer is = " + answer + "\nContinue?");
  59. if (scanner.next().equalsIgnoreCase("yes")) {
  60. cont = true;
  61. } else {
  62. cont = false;
  63. }
  64. }
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement