Sininerebane

Untitled

Dec 27th, 2021
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. package Mine.Domzadanie;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Calculator {
  6. public static void main(String[] args) {
  7. int num1 = getInt();
  8. char symbol = getOperation();
  9. int num2 = getInt();
  10. calc(num1, num2, symbol);
  11. }
  12.  
  13. public static int getInt() {
  14. System.out.println("Введите число:");
  15. Scanner scan = new Scanner(System.in);
  16. int number = scan.nextInt();
  17. return number;
  18. }
  19.  
  20. public static char getOperation() {
  21. System.out.println("Введите символ: + , - , *, /");
  22. Scanner myscan = new Scanner(System.in);
  23. char c = myscan.next().charAt(0);
  24. return c;
  25. }
  26.  
  27. public static int calc(int numr1, int numr2, char operation) {
  28. int result = 0;
  29. String resultMessage = "";
  30. switch (operation) {
  31. case '+':
  32. result = numr1 + numr2;
  33. break;
  34. case '-':
  35. result = numr1 - numr2;
  36. break;
  37. case '*':
  38. result = numr1 * numr2;
  39. break;
  40. case '/':
  41. result = numr1 / numr2;
  42. break;
  43. default:
  44. resultMessage = "Invalid operator!";
  45.  
  46. }
  47. if (resultMessage.length() > 1) {
  48. System.out.println(resultMessage);
  49.  
  50. } else {
  51. System.out.println(result);
  52. }
  53. return result;
  54. }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment