返回列表 發帖
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. public class A02 {
  4.         public static void main(String[] args)
  5.         {
  6.                 int a, b, s=0;
  7.                 while(true)
  8.                 {
  9.                         if(s==3)
  10.                         {
  11.                                 System.out.print("嘗試次數過多");
  12.                                 return;
  13.                         }
  14.                         try
  15.                         {
  16.                                 Scanner c=new Scanner(System.in);
  17.                                 System.out.print("請輸入分子: ");
  18.                                 a=c.nextInt();
  19.                                 System.out.print("請輸入分母: ");
  20.                                 b=c.nextInt();
  21.                                 System.out.print(a+"/"+b+"="+(a/b));
  22.                         }catch(ArithmeticException e)
  23.                         {
  24.                                 s++;
  25.                                 System.out.println("分母不可為零");

  26.                         }catch(InputMismatchException e)
  27.                         {
  28.                                 s++;
  29.                                 System.out.println("請輸入整數");
  30.                         }
  31.                 }
  32.         }
  33. }
複製代碼

TOP

返回列表