Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zajęcia3;
- import java.util.Scanner;
- public class wyjatek_cw {
- public static void main(String[] args) {
- while(true) {
- try {
- funkcja();
- break;
- }
- catch(NieLiczba e) {
- System.out.println(e.getMessage());
- continue;
- }
- catch(Ujemna e) {
- System.out.println(e.getMessage());
- continue;
- }
- }
- }
- private static void funkcja() throws NieLiczba, Ujemna{
- Scanner sc = new Scanner(System.in);
- System.out.println("Podaj liczbę: ");
- while(sc.hasNextDouble()) {
- double liczba = sc.nextDouble();
- if(liczba > 0)
- System.out.println("Pierwiastek z liczby " + liczba + " wynosi: " + pierwiastek(liczba));
- else
- throw new Ujemna();
- }
- throw new NieLiczba();
- }
- private static double pierwiastek(double x) {
- return Math.sqrt(x);
- }
- }
- class NieLiczba extends Exception{
- public NieLiczba() {
- super("Nie podałeś liczby!!!");
- }
- }
- class Ujemna extends Exception{
- public Ujemna() {
- super("Podałeś liczbę ujemną! Nie można z niej wyciągnąć pierwiastka :(");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement