Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) throws Exception {
  5. // 『スラスラわかるJava』
  6. // 第14章-1 例外
  7.  
  8. Scanner sc = new Scanner(System.in);
  9.  
  10. try{
  11. System.out.println("割る数を入力してください :");
  12. double num1 = sc.nextDouble();
  13.  
  14. System.out.println("割られる数を入力してください:");
  15. double num2 = sc.nextDouble();
  16.  
  17. double result = divide(num1,num2);
  18.  
  19. System.out.println(num1 + " ÷ " + num2 + " = " + result);
  20. } catch (InputMismatchException e){
  21. System.out.println("不正な入力です。数値を入力してください。");
  22. } catch (ArithmeticException e){
  23. System.out.println("0で割ることはできません。");
  24. }
  25.  
  26.  
  27. }
  28. static double divide(double num1, double num2){
  29. if (num2 == 0.0){
  30. throw new ArithmeticException();
  31. }else{
  32. //0割りでないときには何もしない
  33. }
  34. return num1 / num2;
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement