Advertisement
Morogn93

Bartosz Wilk

Mar 25th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.util.InputMismatchException;
  2. import java.util.Locale.Category;
  3. import java.util.Scanner;
  4.  
  5. /* ##################### BARTOSZ WILCZYŃSKI####################*/
  6. /* #####################     KALKULATOR    ####################*/
  7.  
  8. public class JavaClass {
  9.     public static void main(String[] args) {
  10.         Logic run = new Logic();
  11.        
  12.     }
  13. }
  14. class Logic {  
  15.     Scanner scaner;
  16.     public Logic() {
  17.         char temp = ' ';
  18.         while(temp!=('q')) {
  19.             scaner = new Scanner(System.in);
  20.             TimeToChose();                                 
  21.             WyswietlNapis("Jesli chcesz wyjsc z programu wpisz \"q\" jesli chcesz kontynuowac wprowadz dowolny inny znak ");
  22.             temp = scaner.next().charAt(0);
  23.            
  24.         }
  25.         scaner.close();
  26.     }
  27.        
  28.    
  29.     public static int Dodaj(int a, int b) {
  30.         return a+b;
  31.     }  
  32.     public static int Odejmij(int a, int b) {
  33.         return a - b;
  34.     }
  35.     public static int Pomnoz(int a, int b) {
  36.         return a*b;
  37.     }
  38.     public static int Podziel(int a, int b) {
  39.         if(b == 0) {
  40.             throw new ArithmeticException("Dzielenie przez zero");
  41.         }else {
  42.             return a/b;
  43.         }
  44.     }  
  45.     public static void WyswietlNapis(String napis) {
  46.         System.out.println(napis);
  47.     }
  48.     public static void WyswietlNapis(int napis) {
  49.         System.out.println(napis);
  50.     }
  51.    
  52.     public static void TimeToChose() {
  53.         int a = 0;
  54.         int b = 0;
  55.         String operator = "";
  56.         @SuppressWarnings("resource")
  57.         Scanner czytnik = new Scanner(System.in);
  58.             try {
  59.            
  60.             WyswietlNapis("Podaj pierwszą wartość");
  61.             a = czytnik.nextInt();
  62.             WyswietlNapis("Podaj operator");
  63.             operator = czytnik.next();
  64.             WyswietlNapis("Podaj drugą wartość");
  65.             b = czytnik.nextInt();
  66.             }catch(InputMismatchException e) {
  67.                 System.out.println("podałeś zły znak");
  68.             }
  69.             try {
  70.                 if (operator.equals("+")) {
  71.                     WyswietlNapis(Dodaj(a, b));
  72.                 }
  73.                 else if (operator.equals("-")) {
  74.                     WyswietlNapis(Odejmij(a, b));
  75.                 }
  76.                 else if (operator.equals("@")) {
  77.                     WyswietlNapis(Pomnoz(a, b));
  78.                 }
  79.                 else if (operator.equals("/")) {
  80.                     WyswietlNapis(Podziel(a, b));
  81.                 } else {
  82.                     throw new Exception("Podałeś zły znak jako operator");
  83.                 }
  84.             }catch(Exception e){
  85.                 System.out.println(e);
  86.             }      
  87.     }
  88. }
  89.    
  90. /* ##################### BARTOSZ WILCZYŃSKI####################*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement