Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SecondJavaApp {
  4.  
  5.    public static double calc()
  6.    {
  7.         double firstNum;
  8.         double secondNum;
  9.         char operation;
  10.         double result = 0;
  11.  
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         System.out.println("Proszę podaj w oddzielnych linijkach jakąś liczbę, operację matematyczną +,-,*,/,%, a następnie kolejną liczbę:");
  15.  
  16.         firstNum = scanner.nextDouble();
  17.         operation = scanner.next().charAt(0);
  18.         secondNum = scanner.nextDouble();
  19.  
  20.        switch(operation)
  21.        {
  22.            case '+':
  23.            {
  24.                result = firstNum + secondNum;
  25.                break;
  26.            }
  27.            case '-':
  28.            {
  29.                result = firstNum - secondNum;
  30.                break;
  31.            }
  32.            case '*':
  33.            {
  34.                result = firstNum * secondNum;
  35.                break;
  36.            }
  37.            case '/':
  38.            {
  39.                if (secondNum != 0)
  40.                {
  41.                    result = firstNum / secondNum;
  42.                } else {
  43.                    System.out.println("Nie dzielimy przez zero!!");
  44.                }
  45.                break;
  46.            }
  47.            case '%':
  48.            {
  49.                if (secondNum != 0)
  50.                {
  51.                    result = firstNum % secondNum;
  52.                } else {
  53.                    System.out.println("Nie dzielimy przez zero!!");
  54.                }
  55.            }
  56.        }
  57.  
  58.            System.out.println("Twój wynik to:" + result);
  59.            System.out.println("Chcesz wykonać kolejne działanie? Wpisz literę t lub n. ");
  60.            char repeat = scanner.next().charAt(0);
  61.  
  62.        switch(repeat)
  63.        {
  64.            case 't':
  65.            {
  66.                SecondJavaApp.calc();
  67.                break;
  68.            }
  69.            case 'n':
  70.            {
  71.                System.out.println("Koniec");
  72.                break;
  73.            }
  74.  
  75.        }
  76.  
  77.         scanner.close();
  78.         return 0;
  79.     }
  80.  
  81.  
  82.     public static void main(String[] args)
  83.     {
  84.         SecondJavaApp.calc();
  85.  
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement