返回列表 發帖
本帖最後由 林羿丞 於 2021-1-16 11:23 編輯
  1. package aaa;

  2. import java.util.*;

  3. public class aaa
  4. {

  5.     public static void main(String[] args) throws MyException
  6.     {
  7.      
  8.         
  9.          
  10.             
  11.                 Scanner s=new Scanner(System.in);
  12.                 int x,y;
  13.                 System.out.print("輸入分子: ");
  14.                 x=s.nextInt();
  15.                 System.out.print("輸入分母: ");
  16.                 y=s.nextInt();
  17.                 if(y==0)
  18.                     throw new MyException("嘿嘿嘿~分母不可為零喔!");
  19.                 System.out.println(x+"/"+y+"="+(x/y));
  20.         }
  21.     }

  22. class MyException extends Exception
  23.                 {
  24.                     MyException(String str)
  25.                     {
  26.                         super(str);   //呼叫父類別的建構子
  27.                     }
  28.                 }
複製代碼

TOP

返回列表