返回列表 發帖
  1. import java.util.*;
  2. public class ch52
  3. {
  4.     public static void main(String args[])
  5.     {
  6.          for(int i=1; i<=3; i++)
  7.          {
  8.              try
  9.              {
  10.                  Scanner s=new Scanner(System.in);
  11.                  int a, b;
  12.                  System.out.print("請輸入分子: ");
  13.                  a=s.nextInt();
  14.                  System.out.print("請輸入分母: ");
  15.                  b=s.nextInt();
  16.                  System.out.println(a+"/"+b+"="+(a/b));
  17.                  return;
  18.              }
  19.              catch(ArithmeticException e)
  20.              {
  21.                  System.out.println("運算錯誤! 分母不可為零!");
  22.              }
  23.              catch(InputMismatchException e)
  24.              {
  25.                  System.out.println("格式錯誤! 輸入須為整數!");
  26.              }
  27.              if(i==3)
  28.                  System.out.println("錯誤嘗試過多! 程式跳出!");
  29.              System.out.println();
  30.          }
  31.     }
  32. }
複製代碼

TOP

返回列表