HarryJW

Java Calculator (Updated)

Jan 4th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. package javaCalculator;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Calculator {
  6.  
  7. public static void main (String [] args) {
  8.  
  9.  
  10. System.out.println("Please enter your first number");
  11. Scanner Scanner1 = new Scanner (System.in);
  12. int firstInteger = Scanner1.nextInt();
  13.  
  14.  
  15. System.out.println("Please enter your second number");
  16. Scanner Scanner2 = new Scanner (System.in);
  17. int secondInteger = Scanner2.nextInt();
  18.  
  19. System.out.println("Please enter the operation");
  20. Scanner Scanner3 = new Scanner (System.in);
  21. String userOperation = Scanner3.next();
  22. String additionOperation = "+";
  23. String subtractionOperation = "-";
  24. String multiplacationOperation = "*";
  25. String divisionOperation = "/";
  26. boolean addition = userOperation.equals(additionOperation);
  27. boolean subtract = userOperation.equals(subtractionOperation);
  28. boolean multiplacation = userOperation.equals(multiplacationOperation);
  29. boolean division = userOperation.equals(divisionOperation);
  30.  
  31. int answer = 0;
  32. if (addition){
  33.  
  34. answer = (firstInteger+secondInteger);
  35.  
  36. System.out.print(answer);
  37.  
  38. }else{
  39.  
  40. if (subtract) {
  41.  
  42. answer = (firstInteger-secondInteger);
  43.  
  44. System.out.print(answer);
  45.  
  46. }else{
  47.  
  48. if (multiplacation) {
  49.  
  50. answer = (firstInteger*secondInteger);
  51.  
  52. System.out.print(answer);
  53.  
  54.  
  55. }else{
  56.  
  57. if (division) {
  58.  
  59. answer = (firstInteger/secondInteger);
  60.  
  61.  
  62.  
  63. }else{
  64.  
  65.  
  66. System.out.print(answer);
  67.  
  68. }
  69. Scanner1.close();
  70. Scanner2.close();
  71. Scanner3.close();
  72. }
  73.  
  74. }}}}
Advertisement
Add Comment
Please, Sign In to add comment