返回列表 發帖
  1. import java.util.*;
  2. public class Ch17 {

  3.         public static void main(String[] args)
  4.         {
  5.                 int x,y,count=0;
  6.                 while(true)
  7.                 {
  8.                         if(count==3)
  9.                         {
  10.                                 System.out.println("您已輸入錯誤三次了!");
  11.                                 return;
  12.                         }
  13.                         try
  14.                         {
  15.                         Scanner c= new Scanner(System.in);
  16.                         System.out.print("請輸入x:");
  17.                         x=c.nextInt();
  18.                         System.out.print("請輸入y:");
  19.                         y=c.nextInt();
  20.                         System.out.print("x/y="+(x/y));
  21.                         }catch(ArithmeticException e)
  22.                         {
  23.                                 count++;
  24.                                 System.out.println("分母不可以為零!");
  25.                         }catch(InputMismatchException e)
  26.                         {
  27.                                 count++;
  28.                                 System.out.println("輸入必須為整數!");
  29.                         }
  30.                 }
  31.         }

  32. }
複製代碼

TOP

返回列表