返回列表 發帖
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;


  3. public class Hello {

  4.         public static void main(String[] args) {
  5.                 // TODO 自動產生的方法 Stub

  6.                 int x ,y ,n=0;
  7.                 for(;;)
  8.                 {
  9.                         if(n==3)
  10.                         {
  11.                                 System.out.println("錯誤過多");
  12.                                 return;
  13.                         }
  14.                         try
  15.                         {
  16.                                 Scanner s=new Scanner(System.in);
  17.                                 System.out.print("請輸入x:");
  18.                                 x=s.nextInt();
  19.                                 System.out.print("請輸入y:");
  20.                                 y=s.nextInt();
  21.                                 System.out.println("x/y="+(x/y));
  22.                         }catch(ArithmeticException e)
  23.                         {
  24.                                 n++;
  25.                                 System.out.println("運算錯誤! 分母不可為零!");
  26.                         }catch(InputMismatchException e)
  27.                         {
  28.                                 n++;
  29.                                 System.out.println("格式錯誤! 輸入須為整數!");
  30.                         }
  31.                 }
  32.         }

  33. }
複製代碼

TOP

返回列表