返回列表 發帖
本帖最後由 李彣 於 2023-7-5 20:05 編輯
  1. import java.util.Scanner;
  2. import java.util.InputMismatchException;
  3. public class B
  4. {
  5.         public static void main(String[] args)
  6.         {
  7.                 try
  8.         {
  9.             Scanner s=new Scanner(System.in);
  10.             int x,y;
  11.             System.out.print("輸入分子:");
  12.             x=s.nextInt();
  13.             System.out.print("輸入分母:");
  14.             y=s.nextInt();
  15.             System.out.println(x+"/"+y+"="+(x/y));
  16.         }
  17.                 catch(InputMismatchException e)
  18.         {
  19.             System.out.println("格式錯誤!輸入須為整數!");
  20.             System.out.println("例外類別:"+e.toString());
  21.         }
  22.                 catch(ArithmeticException e)
  23.         {
  24.             System.out.println("運算錯誤! 分母不可為零!");
  25.             System.out.println("例外類別:"+e.toString());
  26.         }
  27.         System.out.println();
  28.         }
  29. }
複製代碼

TOP

返回列表