Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SecondJavaApp {
- public static void calc()
- {
- double firstNum;
- double secondNum;
- char operation;
- double result = 0;
- Scanner scanner = new Scanner(System.in);
- System.out.println("Proszę podaj w oddzielnych linijkach jakąś liczbę, operację matematyczną +,-,*,/,%, a następnie kolejną liczbę:");
- firstNum = scanner.nextDouble();
- operation = scanner.next().charAt(0);
- secondNum = scanner.nextDouble();
- switch(operation)
- {
- case '+':
- {
- result = firstNum + secondNum;
- break;
- }
- case '-':
- {
- result = firstNum - secondNum;
- break;
- }
- case '*':
- {
- result = firstNum * secondNum;
- break;
- }
- case '/':
- {
- if (secondNum != 0)
- {
- result = firstNum / secondNum;
- } else {
- System.out.println("Nie dzielimy przez zero!!");
- }
- break;
- }
- case '%':
- {
- if (secondNum != 0)
- {
- result = firstNum % secondNum;
- } else {
- System.out.println("Nie dzielimy przez zero!!");
- }
- }
- default:
- {
- System.out.println("Zły znak, popraw się!!");
- }
- }
- System.out.println("Twój wynik to:" + result);
- System.out.println("Chcesz wykonać kolejne działanie? Wpisz literę t lub n. ");
- char repeat = scanner.next().charAt(0);
- switch(repeat)
- {
- case 't':
- {
- SecondJavaApp.calc();
- break;
- }
- case 'n':
- {
- System.out.println("Koniec");
- break;
- }
- default:
- {
- System.out.println("Zły znak, kończe program");
- }
- }
- scanner.close();
- }
- public static void main(String[] args)
- {
- SecondJavaApp.calc();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement