Advertisement
bobo_bobkata

Untitled

Nov 27th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. package NestedConditionalStatements;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class OperationsBetweenNumbers {
  6. public static void main(String[] args) {
  7.  
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. int num1 = Integer.parseInt(scanner.nextLine());
  11. int num2 = Integer.parseInt(scanner.nextLine());
  12. char operation = scanner.nextLine().charAt(0);
  13.  
  14. String type = "";
  15.  
  16. switch (operation) {
  17. case '+':
  18. int sum = num1 + num2;
  19.  
  20. if (sum % 2 == 0) {
  21. type = "even";
  22. } else {
  23. type = "odd";
  24. }
  25. System.out.printf("%d %c %d = %d - %s", num1, operation, num2, sum, type);
  26. break;
  27. case '-':
  28. int diff = num1 - num2;
  29. if (diff % 2 == 0) {
  30. type = "even";
  31. } else {
  32. type = "odd";
  33. }
  34. System.out.printf("%d %c %d = %d - %s", num1, operation, num2, diff, type);
  35. break;
  36. case '*':
  37. int product = num1 * num2;
  38. if (product % 2 == 0) {
  39. type = "even";
  40. } else {
  41. type = "odd";
  42. }
  43. System.out.printf("%d %c %d = %d - %s", num1, operation, num2, product, type);
  44. break;
  45. case '/':
  46. double division = num1 * 1.0 / num2;
  47. if (num2 != 0) {
  48. System.out.printf("%d %c %d = %.2f ", num1, operation, num2, division);
  49. }else{
  50. System.out.printf("Cannot divide %d by zero", num1);
  51. }
  52. break;
  53. case '%':
  54.  
  55. if (num2 != 0) {
  56. int left = num1 % num2;
  57. System.out.printf("%d %c %d = %d ", num1, operation, num2, left);
  58. }else{
  59. System.out.printf("Cannot divide %d by zero", num1);
  60. }
  61. break;
  62.  
  63. }
  64.  
  65.  
  66. }
  67.  
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement