AntoniiaG

Untitled

Feb 18th, 2022 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package Methods.lab;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MathOperations {
  6.     public static void main(String[] args) {
  7.         //Write a method that receives two numbers and an operator, calculates the result, and returns it. You will be given
  8.         //three lines of input. The first will be the first number, the second one will be the operator and the last one will be
  9.         //the second number. The possible operators are: / * + -
  10.         //Print the result rounded up to the second decimal point.
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         int number1 = Integer.parseInt(scanner.nextLine());
  14.         String symbol = scanner.nextLine();
  15.         int number2 = Integer.parseInt(scanner.nextLine());
  16.  
  17. //      printOperation(number1, symbol, number2);
  18.  
  19.         switch (symbol) {
  20.             case "+":
  21.                 calculationOperationAdd(number1, number2);
  22.                 break;
  23.             case "*":
  24.                 calculationOperationMultiply(number1, number2);
  25.                 break;
  26.             case "-":
  27.                 calculationOperationSubtract(number1, number2);
  28.                 break;
  29.             case "/":
  30.                 calculationOperationDivide(number1, number2);
  31.                 break;
  32.         }
  33.     }
  34.  
  35. static void printOperation(int number1, String symbol, int number2){
  36. //        int sum = 0;
  37. //        switch (symbol) {
  38. //            case "+":
  39. //                sum = number1 + number2;
  40. //                break;
  41. //            case "*":
  42. //                sum = number1 * number2;
  43. //                break;
  44. //            case "-":
  45. //                sum = number1 - number2;
  46. //                break;
  47. //            case "/":
  48. //                sum = number1 / number2;
  49. //                break;
  50. //        }
  51. //        System.out.println(sum);
  52. //
  53. //    }
  54.  
  55.     static void calculationOperationAdd (int number1, int number2){
  56.         int sum = number1 + number2;
  57.         System.out.println(sum);
  58.     }
  59.  
  60.     static void calculationOperationMultiply(int number1, int number2){
  61.         int sum = number1 * number2;
  62.         System.out.println(sum);
  63.     }
  64.  
  65.     static void calculationOperationSubtract (int number1, int number2){
  66.         int sum = number1 - number2;
  67.         System.out.println(sum);
  68.     }
  69.  
  70.     static void calculationOperationDivide (int number1, int number2){
  71.         int sum = number1 / number2;
  72.         System.out.println(sum);
  73.     }
  74. }
  75.  
  76.  
  77.  
  78.  
Add Comment
Please, Sign In to add comment