Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- import java.util.Locale.Category;
- import java.util.Scanner;
- /* ##################### BARTOSZ WILCZYŃSKI####################*/
- /* ##################### KALKULATOR ####################*/
- public class JavaClass {
- public static void main(String[] args) {
- Logic run = new Logic();
- }
- }
- class Logic {
- Scanner scaner;
- public Logic() {
- char temp = ' ';
- while(temp!=('q')) {
- scaner = new Scanner(System.in);
- TimeToChose();
- WyswietlNapis("Jesli chcesz wyjsc z programu wpisz \"q\" jesli chcesz kontynuowac wprowadz dowolny inny znak ");
- temp = scaner.next().charAt(0);
- }
- scaner.close();
- }
- public static int Dodaj(int a, int b) {
- return a+b;
- }
- public static int Odejmij(int a, int b) {
- return a - b;
- }
- public static int Pomnoz(int a, int b) {
- return a*b;
- }
- public static int Podziel(int a, int b) {
- if(b == 0) {
- throw new ArithmeticException("Dzielenie przez zero");
- }else {
- return a/b;
- }
- }
- public static void WyswietlNapis(String napis) {
- System.out.println(napis);
- }
- public static void WyswietlNapis(int napis) {
- System.out.println(napis);
- }
- public static void TimeToChose() {
- int a = 0;
- int b = 0;
- String operator = "";
- @SuppressWarnings("resource")
- Scanner czytnik = new Scanner(System.in);
- try {
- WyswietlNapis("Podaj pierwszą wartość");
- a = czytnik.nextInt();
- WyswietlNapis("Podaj operator");
- operator = czytnik.next();
- WyswietlNapis("Podaj drugą wartość");
- b = czytnik.nextInt();
- }catch(InputMismatchException e) {
- System.out.println("podałeś zły znak");
- }
- try {
- if (operator.equals("+")) {
- WyswietlNapis(Dodaj(a, b));
- }
- else if (operator.equals("-")) {
- WyswietlNapis(Odejmij(a, b));
- }
- else if (operator.equals("@")) {
- WyswietlNapis(Pomnoz(a, b));
- }
- else if (operator.equals("/")) {
- WyswietlNapis(Podziel(a, b));
- } else {
- throw new Exception("Podałeś zły znak jako operator");
- }
- }catch(Exception e){
- System.out.println(e);
- }
- }
- }
- /* ##################### BARTOSZ WILCZYŃSKI####################*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement