Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Methods.lab;
- import java.util.Scanner;
- public class MathOperations {
- public static void main(String[] args) {
- //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.
- Scanner scanner = new Scanner(System.in);
- int number1 = Integer.parseInt(scanner.nextLine());
- String symbol = scanner.nextLine();
- int number2 = Integer.parseInt(scanner.nextLine());
- // printOperation(number1, symbol, number2);
- switch (symbol) {
- case "+":
- calculationOperationAdd(number1, number2);
- break;
- case "*":
- calculationOperationMultiply(number1, number2);
- break;
- case "-":
- calculationOperationSubtract(number1, number2);
- break;
- case "/":
- calculationOperationDivide(number1, number2);
- break;
- }
- }
- static void printOperation(int number1, String symbol, int number2){
- // int sum = 0;
- // switch (symbol) {
- // case "+":
- // sum = number1 + number2;
- // break;
- // case "*":
- // sum = number1 * number2;
- // break;
- // case "-":
- // sum = number1 - number2;
- // break;
- // case "/":
- // sum = number1 / number2;
- // break;
- // }
- // System.out.println(sum);
- //
- // }
- static void calculationOperationAdd (int number1, int number2){
- int sum = number1 + number2;
- System.out.println(sum);
- }
- static void calculationOperationMultiply(int number1, int number2){
- int sum = number1 * number2;
- System.out.println(sum);
- }
- static void calculationOperationSubtract (int number1, int number2){
- int sum = number1 - number2;
- System.out.println(sum);
- }
- static void calculationOperationDivide (int number1, int number2){
- int sum = number1 / number2;
- System.out.println(sum);
- }
- }
Add Comment
Please, Sign In to add comment