Advertisement
Morogn93

Untitled

Mar 26th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 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.            
  20.            
  21.             scaner = new Scanner(System.in);
  22.             TimeToChose();                                 
  23.             WyswietlNapis("Jesli chcesz wyjsc z programu wpisz \"q\" ");
  24.             temp = scaner.next().charAt(0);
  25.            
  26.         }
  27.         scaner.close();
  28.     }
  29.        
  30.    
  31.     public static int Dodaj(int a, int b) {
  32.         return a+b;
  33.     }  
  34.     public static int Odejmij(int a, int b) {
  35.         return a - b;
  36.     }
  37.     public static int Pomnoz(int a, int b) {
  38.         return a*b;
  39.     }
  40.     public static int Podziel(int a, int b) {
  41.         if(b == 0) {
  42.             throw new ArithmeticException("Dzielenie przez zero");
  43.         }else {
  44.             return a/b;
  45.         }
  46.     }  
  47.     public static void WyswietlNapis(String napis) {
  48.         System.out.println(napis);
  49.     }
  50.     public static void WyswietlNapis(int napis) {
  51.         System.out.println(napis);
  52.     }
  53.    
  54.     public static void TimeToChose() {
  55.         int a = 0;
  56.         int b = 0;
  57.         String operator = "";
  58.         @SuppressWarnings("resource")
  59.         Scanner czytnik = new Scanner(System.in);
  60.             try {
  61.            
  62.             WyswietlNapis("Podaj pierwszą wartość");
  63.             a = czytnik.nextInt();
  64.             WyswietlNapis("Podaj operator");
  65.             WyswietlNapis("(+) dodawanie:");
  66.             WyswietlNapis("(-) odejmowanie:");
  67.             WyswietlNapis("(@) mnozenie:");
  68.             WyswietlNapis("(/) dzielenie:");
  69.             WyswietlNapis("oraz druga liczbe");
  70.            
  71.             operator = czytnik.next();
  72.             WyswietlNapis("Podaj drugą wartość");
  73.             b = czytnik.nextInt();
  74.             }catch(InputMismatchException e) {
  75.                 System.out.println("podałeś zły znak");
  76.             }
  77.             try {
  78.                 if (operator.equals("+")) {
  79.                     WyswietlNapis(Dodaj(a, b));
  80.                 }
  81.                 else if (operator.equals("-")) {
  82.                     WyswietlNapis(Odejmij(a, b));
  83.                 }
  84.                 else if (operator.equals("@")) {
  85.                     WyswietlNapis(Pomnoz(a, b));
  86.                 }
  87.                 else if (operator.equals("/")) {
  88.                     WyswietlNapis(Podziel(a, b));
  89.                 } else {
  90.                     throw new Exception("Podałeś zły znak jako operator");
  91.                 }
  92.             }catch(Exception e){
  93.                 System.out.println(e);
  94.             }      
  95.     }
  96. }
  97.    
  98. /* ##################### BARTOSZ WILCZYŃSKI####################*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement