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

TOP

返回列表