Advertisement
EChebanenko

Untitled

Oct 29th, 2020
2,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package HwLesson15;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Logic {
  6.     static Scanner scanner = new Scanner(System.in);
  7.  
  8.     static void main(String[] args) {
  9.         int num1 = getNum();
  10.         int num2 = getNum();
  11.         Methods operation = getOperation();
  12.         int result = result(num1, num2, operation);
  13.         System.out.println("Результат операции: " + result);
  14.     }
  15.  
  16.     static int getNum() {
  17.         System.out.println("Введите число:");
  18.         int num;
  19.         if (scanner.hasNextInt()) {
  20.             num = scanner.nextInt();
  21.         } else {
  22.             System.out.println("Вы допустили ошибку при вводе числа. Попробуйте еще раз.");
  23.             scanner.next();
  24.             num = getNum();
  25.         }
  26.         return num;
  27.     }
  28.  
  29.     static Methods getOperation() {
  30.         Scanner sc = new Scanner(System.in);
  31.         Methods operation;
  32.         System.out.println("Введите операцию:");
  33.  
  34.         String tmp = sc.nextLine();
  35.         switch (tmp) {
  36.             case "+":
  37.                 operation = Methods.Plus;
  38.                 break;
  39.  
  40.             case "-":
  41.                 operation = Methods.Minus;
  42.                 break;
  43.  
  44.             case "*":
  45.                 operation = Methods.Multiply;
  46.                 break;
  47.  
  48.             case "/":
  49.                 operation = Methods.Divide;
  50.                 break;
  51.             default:
  52.                 System.out.println("Вы допустили ошибку при вводе операции.");
  53.                 operation = Methods.Wtf;
  54.  
  55.         }
  56.         return operation;
  57.     }
  58.  
  59.     static int result(int num1, int num2, Methods operation) {
  60.         int result;
  61.  
  62.  
  63.         switch (operation) {
  64.             case Plus:
  65.                 result = num1 + num2;
  66.                 break;
  67.             case Minus:
  68.                 result = num1 - num2;
  69.                 break;
  70.             case Multiply:
  71.                 result = num1 * num2;
  72.                 break;
  73.             case Divide:
  74.                 result = num1 / num2;
  75.                 break;
  76.             default:
  77.                 System.out.println("Операция не распознана.");
  78.                 result = result(num1, num2, getOperation());
  79.         }
  80.         return result;
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement