返回列表 發帖
  1. import java.util.Scanner;
  2. public class OK {
  3.          static Scanner s=new Scanner(System.in);
  4.         public static void main(String[] args)  throws MyException{
  5.                 // TODO 自動產生的方法 Stub
  6.             
  7.           
  8.        
  9.                 float x,y;
  10.                 System.out.print("輸入分子: ");
  11.                 x=s.nextFloat();
  12.                 System.out.print("輸入分母: ");
  13.                 y=s.nextFloat();
  14.                 if(y==0)
  15.                     throw new MyException("嘿嘿嘿~分母不可為零喔!");
  16.                 System.out.println(x+"/"+y+"="+(x/y));
  17.             }
  18.         }
  19.         class MyException extends Exception
  20.         {
  21.             MyException(String str)
  22.             {
  23.                 super(str);   //呼叫父類別的建構子
  24.         }

  25. }
複製代碼

TOP

返回列表