返回列表 發帖
本帖最後由 林劭澧 於 2023-7-26 20:00 編輯
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. public class Ch01
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.             int n=0;
  8.             while(true)
  9.             {
  10.                 if(n==3)
  11.                     {
  12.                          System.out.println("錯誤嘗試過多,程式跳出!");
  13.                          return;
  14.                     }
  15.              try
  16.              {
  17.                            
  18.                             Scanner s=new Scanner(System.in);
  19.                 int x,y;
  20.                 System.out.print("輸入分子: ");
  21.                 x=s.nextInt();
  22.                 System.out.print("輸入分母: ");
  23.                 y=s.nextInt();
  24.                 System.out.println(x+"/"+y+"="+(x/y));
  25.             }
  26.                 catch(ArithmeticException e)
  27.             {
  28.                 System.out.println("運算錯誤!分母不可為0");
  29.                         n++;
  30.             }
  31.                 catch(InputMismatchException e)
  32.             {
  33.                 System.out.println("程式錯誤!請輸入整數!");
  34.                 n++;
  35.             }
  36.         }
  37.     }
  38. }
複製代碼

TOP

返回列表