返回列表 發帖
  1. import java.util.Scanner;
  2. import java.util.InputMismatchException;
  3. public class Test
  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.                  {
  14.                 try
  15.         {
  16.                Scanner s=new Scanner(System.in);
  17.                int i,j;
  18.                System.out.print("輸入分子:");
  19.                i=s.nextInt();
  20.                System.out.print("輸入分母:");
  21.                j=s.nextInt();
  22.                System.out.println(i+"/"+j+"="+(i/j))
  23.         }catch(ArithmeticException e)
  24.                 {
  25.                 n++;
  26.                 }
  27.                 System.out.println("運算錯誤! 分母不可為零!\n");
  28.             }catch(InputMismatchException e)
  29.             {
  30.                 n++;
  31.                 System.out.println("格式錯誤! 輸入須為整數!\n");
  32.             }
  33.         }
  34.    }
  35. }
複製代碼

TOP

返回列表