Advertisement
veronikaaa86

06. Operations Between Numbers

Jan 23rd, 2022
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. package advancedConditionalStatements;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P06OperationsBetweenNumbers {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. int numOne = Integer.parseInt(scanner.nextLine());
  10. int numTwo = Integer.parseInt(scanner.nextLine());
  11. char operator = scanner.nextLine().charAt(0);
  12.  
  13. double result = 0;
  14. String evenOrOdd = "";
  15. boolean isZero = false;
  16. if (operator == '+') {
  17. result = numOne + numTwo;
  18. if (result % 2 == 0) {
  19. evenOrOdd = "even";
  20. } else {
  21. evenOrOdd = "odd";
  22. }
  23. } else if (operator == '-') {
  24. result = numOne - numTwo;
  25. if (result % 2 == 0) {
  26. evenOrOdd = "even";
  27. } else {
  28. evenOrOdd = "odd";
  29. }
  30. } else if (operator == '*') {
  31. result = numOne * numTwo;
  32. if (result % 2 == 0) {
  33. evenOrOdd = "even";
  34. } else {
  35. evenOrOdd = "odd";
  36. }
  37. } else if (operator == '/') {
  38. if (numTwo == 0) {
  39. isZero = true;
  40. } else {
  41. result = numOne * 1.0 / numTwo;
  42. }
  43. } else if (operator == '%') {
  44. if (numTwo == 0) {
  45. isZero = true;
  46. } else {
  47. result = numOne % numTwo;
  48. }
  49. }
  50.  
  51. if (operator == '+' || operator == '-' || operator == '*') {
  52. System.out.printf("%d %c %d = %.0f - %s",
  53. numOne, operator, numTwo, result, evenOrOdd);
  54. } else if (operator == '/' && !isZero) {
  55. System.out.printf("%d %c %d = %.2f",
  56. numOne, operator, numTwo, result);
  57. } else if (operator == '%' && !isZero) {
  58. System.out.printf("%d %c %d = %.0f", numOne, operator, numTwo, result);
  59. } else if (isZero && operator == '/' || operator == '%') {
  60. System.out.printf("Cannot divide %d by zero", numOne);
  61. }
  62.  
  63. }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement