返回列表 發帖
  1. import java.util.Scanner;
  2. public class ch51
  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.print(a+"/"+b+"="+a/b);
  17.               }
  18.               catch(java.lang.ArithmeticException e)
  19.               {
  20.                   System.out.println("運算錯誤!分母不可為零!");
  21.               }
  22.               catch(java.util.InputMismatchException e)
  23.               {
  24.                   System.out.println("格式錯誤!輸入需為整數!");
  25.               }
  26.               if(i==3)
  27.                   System.out.println("錯誤嘗試過多!程式跳出!");
  28.           }
  29.     }
  30. }
複製代碼

TOP

返回列表