Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class KalkulatorTester {
- public static void main(String[] args) {
- Kalkulator k = new Kalkulator();
- Scanner num = new Scanner(System.in);
- for(;;) { // nieskończona pętla, bedzie dzialac dopoki uzytkownik nie wcisnie 5
- System.out.println(" ");
- System.out.print("Podaj pierwsza liczbe: ");
- String fNum = num.nextLine(); // pierwsza liczba (first number)
- System.out.print("Podaj druga liczbe: ");
- String sNum = num.nextLine(); // druga liczba (second number)
- k.setLiczby(fNum, sNum); //przekazanie liczb przez argumenty do parametrów
- k.cos();
- System.out.print("Wybierz: ");
- String wybrana = num.nextLine(); // wybranie działania
- k.obliczanie(wybrana); // przekazanie wybranego działania
- }
- }
- }
- class Kalkulator {
- public void cos() {
- System.out.println(" ");
- System.out.println("MENU GLOWNE");
- System.out.println("-----------");
- System.out.println("1. Dodawanie");
- System.out.println("2. Odejmowanie");
- System.out.println("3. Mnożenie");
- System.out.println("4. Dzielenie");
- System.out.println("5. Wyjdź z programu");
- }
- int a, b;
- public void setLiczby(String liczba1, String liczba2) {
- String x = liczba1;
- String y = liczba2;
- a = Integer.parseInt(x);
- b = Integer.parseInt(y);
- }
- public void obliczanie(String wybor) {
- int wyborNum = Integer.parseInt(wybor); //argument był zmienną String, więc zmieniamy ją
- //na zmienną typu int
- int c;
- switch(wyborNum) {
- case 1: c = a + b;
- System.out.println("Suma: " + c );
- break;
- case 2: c = a - b;
- System.out.println("Różnica: " + c);
- break;
- case 3: c = a * b;
- System.out.println("Iloczyn: " + c);
- break;
- case 4: if (b==0) {
- System.out.println("Nie dzielimy przez zero!");
- break;
- } else {
- c = a / b;
- System.out.println("Wynik: " + c);
- break;
- }
- default: System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement