Angel_Kalinkov

ComplexConditionsExamProblems-Operations

Oct 1st, 2017
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.math.RoundingMode;
  3. import java.text.DecimalFormat;
  4. import java.util.Scanner;
  5.  
  6. public class Operations2 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int num1 = Integer.parseInt(scanner.nextLine());
  11.         int num2 = Integer.parseInt(scanner.nextLine());
  12.         String operator = scanner.nextLine();
  13.  
  14.         BigDecimal result = new BigDecimal("0.00");
  15.         String output = "";
  16.         DecimalFormat intFormater = new DecimalFormat("0");
  17.  
  18.         if (num2 == 0 && (operator.equals("/") || operator.equals("%"))) {
  19.             output = String.format("Cannot divide %d by zero", num1);
  20.         } else if (operator.equals("/")) {
  21.             result = new BigDecimal(num1).divide(new BigDecimal(num2), 2, RoundingMode.HALF_UP);
  22.             DecimalFormat decimalFormater = new DecimalFormat("0.00");
  23.             output = String.format("%d %s %d = %s", num1, operator, num2, decimalFormater.format(result));
  24.         } else if (operator.equals("%")) {
  25.             result = new BigDecimal(num1).remainder(new BigDecimal(num2));
  26.             output = String.format("%d %s %d = %s", num1, operator, num2, intFormater.format(result));
  27.         } else {
  28.             if (operator.equals("+")) {
  29.                 result = new BigDecimal(num1 + num2);
  30.             } else if (operator.equals("-")) {
  31.                 result = new BigDecimal(num1 - num2);
  32.             } else if (operator.equals("*")) {
  33.                 result = new BigDecimal(num1 * num2);
  34.             }
  35.             output = String.format("%d %s %d = %s - %s", num1, operator, num2, intFormater.format(result),
  36.                     result.intValueExact() % 2 == 0 ? "even" : "odd");
  37.         }
  38.         System.out.println(output);
  39.     }
  40. }
Add Comment
Please, Sign In to add comment