Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- import static java.lang.Math.pow;
- public class Main {
- public static void znowDzielenie(int x) throws
- ArithmeticException {
- Scanner skaner = new Scanner(System.in);
- int y = skaner.nextInt();
- if (y == 0)
- throw new ArithmeticException("Podaj poprawny mianownik");
- else
- System.out.println("Twรณj wynik to: " + x / y);
- }
- public static class Kalkulator
- {
- float a;
- float b = 0;
- public Kalkulator(float a, float b, float c) {
- this.a = a;
- this.b = b;
- }
- public void wprowadzDane() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Podaj liczbe: ");
- try {
- this.b = scan.nextInt();
- }
- catch (InputMismatchException e)
- {
- System.out.println("Wprowadzono bledne dane.");
- }
- }
- public void dodawanie()
- {
- a += b;
- }
- public void odejmowanie()
- {
- a -= b;
- }
- public void mnozenie()
- {
- a *= b;
- }
- public void dzielenie()
- {
- if (b == 0)
- {
- try
- {
- a = (int)a / (int) b;
- }
- catch (ArithmeticException e)
- {
- System.out.println("Nie dziel przez 0!");
- }
- }
- else a /= (int)b;
- }
- public void potegowanie()
- {
- a = (int)pow(a, b);
- }
- public void pierwiastkowanie()
- {
- try {
- a = (int) pow(a, 1 / b);
- }
- catch (ArithmeticException e)
- {
- System.out.println("Wprowadzono bledne dane.");
- }
- }
- public void reset()
- {
- a = 0;
- }
- public void wyswietlaLiczby()
- {
- System.out.println("a = " + a + "\n");
- }
- public void menu ()
- {
- Scanner scan = new Scanner(System.in);
- int c = 1;
- while (c != 8)
- {
- System.out.println("Wybierz dzialanie: ");
- System.out.println("1 - dodawanie a + b ");
- System.out.println("2 - odejmowanie a - b ");
- System.out.println("3 - mnozenie a * b ");
- System.out.println("4 - dzielenie a / b ");
- System.out.println("5 - potegowanie a ^ b ");
- System.out.println("6 - pierwiastkowanie a ^ (1 / b) ");
- System.out.println("7 - reset");
- System.out.println("8 - wyjscie\n");
- c = scan.nextInt();
- wprowadzDane();
- if (c == 1) dodawanie();
- else if (c == 2) odejmowanie();
- else if (c == 3) mnozenie();
- else if (c == 4) dzielenie();
- else if (c == 5) potegowanie();
- else if (c == 6) pierwiastkowanie();
- else if (c == 7) reset();
- wyswietlaLiczby();
- }
- }
- }
- public static void main(String[] args)
- {
- Kalkulator k = new Kalkulator(0, 0, 0);
- k.menu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement