Advertisement
Guest User

Untitled

a guest
Sep 30th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class operationsBetweenNumbers {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int n1 = Integer.parseInt(scanner.nextLine());
  8. int n2 = Integer.parseInt(scanner.nextLine());
  9. String operator = scanner.nextLine();
  10.  
  11. int output = 0;
  12. boolean ifOdd = true;
  13. boolean ifEven = true;
  14.  
  15. switch (operator){
  16. case "+":
  17. output = n1+n2;
  18. ifOdd = (n1+n2)%2==1;
  19. ifEven = (n1+n2)%2==0;
  20. if (ifOdd){
  21. System.out.printf("%d %s %d = %d - odd",n1,operator,n2,output);
  22. }else if (ifEven){
  23. System.out.printf("%d %s %d = %d - even",n1,operator,n2,output);
  24. }break;
  25. case "-":
  26. output = n1-n2;
  27. ifOdd = (n1-n2)%2==1;
  28. ifEven = (n1-n2)%2==0;
  29. if (ifOdd){
  30. System.out.printf("%d %s %d = %d - odd",n1,operator,n2,output);
  31. }else if (ifEven){
  32. System.out.printf("%d %s %d = %d - even",n1,operator,n2,output);
  33. }break;
  34. case "*":
  35. output = n1*n2;
  36. ifOdd = (n1*n2)%2==1;
  37. ifEven = (n1*n2)%2==0;
  38. if (ifOdd){
  39. System.out.printf("%d %s %d = %d - odd",n1,operator,n2,output);
  40. }else if (ifEven){
  41. System.out.printf("%d %s %d = %d - even",n1,operator,n2,output);
  42. }break;
  43. case "/":
  44. if (n2==0){
  45. System.out.printf("Cannot divide %d by zero",n1);
  46.  
  47. }else {
  48. double f1 = n1;
  49. double f2 = n2;
  50.  
  51. double devidingOutput = f1 / f2;
  52. System.out.printf("%.0f %s %.0f = %.2f",f1,operator,f2,devidingOutput);
  53. }break;
  54. case "%":
  55. if (n2==0){
  56. System.out.printf("Cannot divide %d by zero",n1);
  57.  
  58. }else {
  59. output = n1 % n2;
  60. System.out.printf("%d %s %d = %d", n1, operator, n2, output);
  61. }break;
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement