Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.     static Scanner scanner = new Scanner(System.in);
  6.  
  7.     public static void main(String[] args) {
  8.         int a = gint();
  9.         int b = gint();
  10.         char oper = gop();
  11.         int res = calc(a, b, oper);
  12.         System.out.println("Результат операции: " + res);
  13.     }
  14.  
  15.     public static int gint(){
  16.         System.out.println("Введите число:");
  17.         int num;
  18.         if(scanner.hasNextInt()){
  19.             num = scanner.nextInt();
  20.         } else {
  21.             System.out.println("Вы допустили ошибку при вводе числа");
  22.             scanner.next();
  23.             num = gint();
  24.         }
  25.         return num;
  26.     }
  27.  
  28.     public static char gop(){
  29.         System.out.println("Введите операцию:");
  30.         char operation;
  31.         if(scanner.hasNext()){
  32.             operation = scanner.next().charAt(0);
  33.         } else {
  34.             System.out.println("Вы допустили ошибку при вводе операции");
  35.             scanner.next();
  36.             operation = gop();
  37.         }
  38.         return operation;
  39.     }
  40.  
  41.     public static int calc(int a, int b, char operation){
  42.         int result;
  43.         switch (operation){
  44.             case '+':
  45.                 result = a+b;
  46.                 break;
  47.             case '-':
  48.                 result = a-b;
  49.                 break;
  50.             case '*':
  51.                 result = a*b;
  52.                 break;
  53.             case '/':
  54.                 result = a/b;
  55.                 break;
  56.             default:
  57.                 System.out.println("Операция не распознана");
  58.                 result = calc(a, b, gop());
  59.         }
  60.         return result;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement