返回列表 發帖
  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.                  float a, b;
  12.                  System.out.print("請輸入分子: ");
  13.                  a=s.nextFloat();
  14.                  System.out.print("請輸入分母: ");
  15.                  b=s.nextFloat();
  16.                  if(b==0)
  17.                      throw new ArithmeticException();
  18.                  System.out.println(a+"/"+b+"="+(a/b));
  19.                  return;
  20.              }
  21.              catch(ArithmeticException e)
  22.              {
  23.                  System.out.println("運算錯誤! 分母不可為零!");
  24.              }
  25.              catch(InputMismatchException e)
  26.              {
  27.                  System.out.println("格式錯誤! 輸入須為整數!");
  28.              }
  29.              if(i==3)
  30.                  System.out.println("錯誤嘗試過多! 程式跳出!");
  31.              System.out.println();
  32.          }
  33.     }
  34. }
複製代碼

TOP

返回列表