calculator.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import java.util.*;
  2. public class Calculator {
  3. public static void main(String[] args) {
  4. double displayNum = 0;
  5. boolean calcOn = true;
  6. double num;
  7. Scanner sc = new Scanner(System.in);
  8. while (calcOn) {
  9. System.out.println(displayNum);
  10. System.out.println("Enter an option: ");
  11. System.out.println("0 - ENTER NUMBER");
  12. System.out.println("1 - ADD ");
  13. System.out.println("2 - SUBTRACT");
  14. System.out.println("3 - MULTIPLY");
  15. System.out.println("4 - DIVIDE");
  16. System.out.println("5 - SQUARE");
  17. System.out.println("6 - SQUARE ROOT");
  18. System.out.println("7 - EXPONENT");
  19. System.out.println("8 - Invert +/-");
  20. System.out.println("9 - INVERSE");
  21. System.out.println("10 - SIN");
  22. System.out.println("11 - COS");
  23. System.out.println("12 - TAN");
  24. System.out.println("13 - INVERSE SIN");
  25. System.out.println("14 - INVERSE COS");
  26. System.out.println("15 - INVERSE TAN");
  27. System.out.println("16 - QUIT");
  28. int userInput = sc.nextInt();
  29. //displayNum = userInput;
  30. System.out.println(displayNum);
  31. switch(userInput) {
  32. case 0: System.out.println("enter number");
  33. num = sc.nextDouble();
  34. displayNum = num;
  35. break;
  36. case 1: System.out.println("enter number to ADD to " + displayNum);
  37. num = sc.nextDouble();
  38. displayNum = num + displayNum;
  39. break;
  40. case 2: System.out.println("enter number to SUBTRACT from " + displayNum);
  41. num = sc.nextDouble();
  42. displayNum = displayNum - num;
  43. break;
  44. case 3: System.out.println("enter number to MULTIPLY to" + displayNum);
  45. num = sc.nextDouble();
  46. displayNum = num * displayNum;
  47. break;
  48. case 4: System.out.println("enter number to DIVIDE from " + displayNum);
  49. num = sc.nextDouble();
  50. displayNum = displayNum / num;
  51. break;
  52. case 5: System.out.println("square result: " + displayNum * displayNum);
  53. num = sc.nextDouble();
  54. // displayNum = displayNum * displayNum;
  55. break;
  56. case 6: System.out.println("square root result: " + Math.sqrt(displayNum));
  57. num = sc.nextDouble();
  58. break;
  59. case 7: System.out.println("enter an exponent to the number: " + displayNum);
  60. num = sc.nextDouble();
  61. displayNum = Math.pow(displayNum, num);
  62. break;
  63. case 8: System.out.println("result: " + displayNum * -1);
  64. num = sc.nextDouble();
  65. break;
  66. case 9: System.out.println("result: " + Math.sqrt(displayNum));
  67. num = sc.nextDouble();
  68. // displayNum = displayNum * displayNum;
  69. break;
  70. case 10: System.out.println("result: " + Math.sin(displayNum));
  71. num = sc.nextDouble();
  72. // displayNum = displayNum * displayNum;
  73. break;
  74. case 11: System.out.println("result: " + Math.cos(displayNum));
  75. num = sc.nextDouble();
  76. // displayNum = displayNum * displayNum;
  77. break;
  78. case 12: System.out.println("result: " + Math.tan(displayNum));
  79. num = sc.nextDouble();
  80. // displayNum = displayNum * displayNum;
  81. break;
  82. case 13: System.out.println("result: " + Math.asin(displayNum));
  83. num = sc.nextDouble();
  84. // displayNum = displayNum * displayNum;
  85. break;
  86. case 14: System.out.println("result: " + Math.acos(displayNum));
  87. num = sc.nextDouble();
  88. // displayNum = displayNum * displayNum;
  89. break;
  90. case 15: System.out.println("result: " + Math.atan(displayNum));
  91. num = sc.nextDouble();
  92. // displayNum = displayNum * displayNum;
  93. break;
  94. case 16: System.out.println("Quitting!");
  95. calcOn = false;
  96. break;
  97. default: break;
  98. }
  99. }
  100. }
  101. }