Advertisement
blackpab

Java Zestaw 7 - Zadanie 3v2

Apr 24th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. package javaapplication12;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. //klasa opisujaca wyjatek dzielenia przez zero
  7. class DzieleniePrzezZero extends Exception {
  8.    
  9. }
  10.  
  11. public class Kalkulator {
  12.    
  13.    
  14.     public static void main(String[] args) throws DzieleniePrzezZero {        
  15.                
  16.         Scanner skaner = new Scanner(System.in);        
  17.        
  18.         System.out.println("-== Kalkulator ==-");        
  19.         System.out.println("Dzielenie");        
  20.        
  21.         System.out.print("Dzielna: ");        
  22.         double a = 0;
  23.         try {
  24.             a = skaner.nextDouble();
  25.         } catch(InputMismatchException ex) {
  26.             System.out.println("BLAD: Niepoprawne dane wejsciowe: " + ex);
  27.             return;
  28.             //ex.printStackTrace();
  29.         }    
  30.          
  31.         System.out.print("Dzielnik: ");
  32.         double b = 0;        
  33.         try {
  34.             b = skaner.nextDouble();
  35.         } catch(InputMismatchException ex) {
  36.             System.out.println("BLAD: Niepoprawne dane wejsciowe: " + ex);
  37.             return;
  38.         }
  39.        
  40.         if(b==0) {
  41.             throw new DzieleniePrzezZero();
  42.         }
  43.        
  44.         double c = a/b;
  45.         System.out.println("Iloraz: " + c);        
  46.     }    
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement