Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. /*******************************************************************************
  2. * Author: Benjamin San Nicolas
  3. * Course: CSC240C41
  4. * Date: October 17, 2018
  5. *
  6. * Purpose: The purpose of this program is to request two digits and a operator
  7. * (+,-,*,/) then output the answer after the last item is entered.
  8. ******************************************************************************/
  9. //Header
  10. import java.util.Scanner;
  11.  
  12. //Define class
  13. public class javaCalc
  14. {
  15. //Main Method
  16. public static void main(String[] args)
  17. {
  18. //Variables
  19. int num1, num2;
  20. float ans;
  21. char operation;
  22.  
  23. //Scanner
  24. Scanner input = new Scanner(System.in);
  25.  
  26. //Request operation
  27. do
  28. {
  29. System.out.print("1) Addition\n");
  30. System.out.print("2) Subtraction\n");
  31. System.out.print("3) Mutiplication\n");
  32. System.out.print("4) Division\n");
  33. System.out.print("5) Exit Program\n");
  34. System.out.print("Please choose an operation: ");
  35. operation = input.next().charAt(0);
  36.  
  37. //Switch
  38. switch(operation)
  39. {
  40. //Addition
  41. case '1':
  42. System.out.print("Please enter the first number: ");
  43. num1 = input.nextInt();
  44. System.out.print("Please enter the second number: ");
  45. num2 = input.nextInt();
  46.  
  47. //Calculate and output
  48. ans = num1 + num2;
  49. System.out.printf(num1 + " + " + num2 + " = " + "%.2f", ans);
  50. System.out.print("\n");
  51. System.out.print("\n");
  52. break;
  53.  
  54. //Subtraction
  55. case '2':
  56. System.out.print("Please enter the first number: ");
  57. num1 = input.nextInt();
  58. System.out.print("Please enter the second number: ");
  59. num2 = input.nextInt();
  60.  
  61. //Calculate and output
  62. ans = num1 - num2;
  63. System.out.printf(num1 + " - " + num2 + " = " + "&.2f", ans);
  64. System.out.print("\n");
  65. System.out.print("\n");
  66. break;
  67.  
  68. //Multiplication
  69. case '3':
  70. System.out.print("Please enter the first number: ");
  71. num1 = input.nextInt();
  72. System.out.print("Please enter the second number: ");
  73. num2 = input.nextInt();
  74.  
  75. //Calculate and output
  76. ans = num1 * num2;
  77. System.out.printf(num1 + " * " + num2 + " = " + "%.2f", ans);
  78. System.out.print("\n");
  79. System.out.print("\n");
  80. break;
  81.  
  82. //Division
  83. case '4':
  84. System.out.print("Please enter the first number: ");
  85. num1 = input.nextInt();
  86. System.out.print("Please enter the second number: ");
  87. num2 = input.nextInt();
  88.  
  89. //Calculate and output
  90. ans = num1 / num2;
  91. System.out.printf(num1 + " / " + num2 + " = " + "&.2f", ans);
  92. System.out.print("\n");
  93. System.out.print("\n");
  94. break;
  95.  
  96. //Exit
  97. case '5':
  98. System.exit(0);
  99.  
  100. //Default
  101. default:
  102. System.out.print("Please check your selection.");
  103. }
  104. }
  105. while(operation != 5);
  106.  
  107. }//End Main
  108. }//End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement