Advertisement
CoineTre

JF-LabMethods11.Math Operations

Feb 5th, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1.  
  2. import java.text.DecimalFormat;
  3. import java.util.Scanner;
  4.  
  5. public class Lab11MathOperations {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         double input1 = Double.parseDouble(scanner.nextLine());
  9.         char operation = scanner.nextLine().charAt(0);
  10.         double input2 = Double.parseDouble(scanner.nextLine());
  11.         double calculation = getOperationBySign(input1,operation,input2);
  12.         System.out.println(new DecimalFormat("0.##").format(calculation));
  13.     }
  14.  
  15.     private static double getOperationBySign(double input1, char operation, double input2) {
  16.         double mathCalculation = 0.0;
  17.         switch (operation){
  18.             case'+':
  19.                 mathCalculation = input1+input2;
  20.                 break;
  21.             case'-':
  22.                 mathCalculation = input1-input2;
  23.                 break;
  24.             case'*':
  25.                 mathCalculation = input1*input2;
  26.                 break;
  27.             case'/':
  28.                 mathCalculation = input1/input2;
  29.                 break;
  30.  
  31.         }
  32.         return mathCalculation;
  33.     }
  34. }
  35. /*Write a method that receives two numbers and an operator, calculates the result
  36.  and returns it. You will be given three lines of input. The first will be
  37.   the first number, the second one will be the operator and the last one will
  38.    be the second number. The possible operators are: / * + -
  39. Print the result rounded up to the second decimal point.
  40. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement