Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- static Scanner scanner = new Scanner(System.in);
- public static void main(String[] args) {
- int a = gint();
- int b = gint();
- char oper = gop();
- int res = calc(a, b, oper);
- System.out.println("Результат операции: " + res);
- }
- public static int gint(){
- System.out.println("Введите число:");
- int num;
- if(scanner.hasNextInt()){
- num = scanner.nextInt();
- } else {
- System.out.println("Вы допустили ошибку при вводе числа");
- scanner.next();
- num = gint();
- }
- return num;
- }
- public static char gop(){
- System.out.println("Введите операцию:");
- char operation;
- if(scanner.hasNext()){
- operation = scanner.next().charAt(0);
- } else {
- System.out.println("Вы допустили ошибку при вводе операции");
- scanner.next();
- operation = gop();
- }
- return operation;
- }
- public static int calc(int a, int b, char operation){
- int result;
- switch (operation){
- case '+':
- result = a+b;
- break;
- case '-':
- result = a-b;
- break;
- case '*':
- result = a*b;
- break;
- case '/':
- result = a/b;
- break;
- default:
- System.out.println("Операция не распознана");
- result = calc(a, b, gop());
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement