Advertisement
veronikaaa86

OperatuonsNumbers

Oct 8th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package Exam24April2016;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P03_OperationsNumbers {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int n1 = Integer.parseInt(scanner.nextLine());
  10.         int n2 = Integer.parseInt(scanner.nextLine());
  11.         String o = scanner.nextLine();
  12.  
  13.         int result = 0;
  14.         double resultD = 0.0;
  15.  
  16.         if ("+".equals(o)) {
  17.             result = n1 + n2;
  18.         } else if ("-".equals(o)) {
  19.             result = n1 - n2;
  20.         } else if ("*".equals(o)) {
  21.             result = n1 * n2;
  22.         } else if ("/".equals(o)) {
  23.             resultD = (double) n1 / n2;
  24.             if (n2 == 0) {
  25.                 System.out.printf("Cannot divide %d by zero", n1);
  26.             } else {
  27.                 System.out.printf("%d %s %d = %.2f", n1, o, n2, resultD);
  28.             }
  29.         } else {
  30.             if (n2 == 0) {
  31.                 System.out.printf("Cannot divide %d by zero", n1);
  32.             } else {
  33.                 result = n1 % n2;
  34.                 System.out.printf("%d %s %d = %d", n1, o, n2, result);
  35.             }
  36.         }
  37.  
  38.         if ("+".equals(o) || "-".equals(o) || "*".equals(o)) {
  39.             if (result % 2 == 0) {
  40.                 System.out.printf("%d %s %d = %d - even", n1, o, n2, result);
  41.             } else {
  42.                 System.out.printf("%d %s %d = %d - odd", n1, o, n2, result);
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement