Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class Lab11MathOperations {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double input1 = Double.parseDouble(scanner.nextLine());
- char operation = scanner.nextLine().charAt(0);
- double input2 = Double.parseDouble(scanner.nextLine());
- double calculation = getOperationBySign(input1,operation,input2);
- System.out.println(new DecimalFormat("0.##").format(calculation));
- }
- private static double getOperationBySign(double input1, char operation, double input2) {
- double mathCalculation = 0.0;
- switch (operation){
- case'+':
- mathCalculation = input1+input2;
- break;
- case'-':
- mathCalculation = input1-input2;
- break;
- case'*':
- mathCalculation = input1*input2;
- break;
- case'/':
- mathCalculation = input1/input2;
- break;
- }
- return mathCalculation;
- }
- }
- /*Write a method that receives two numbers and an operator, calculates the result
- and returns it. You will be given three lines of input. The first will be
- the first number, the second one will be the operator and the last one will
- be the second number. The possible operators are: / * + -
- Print the result rounded up to the second decimal point.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement