- import java.util.*;
- public class Ch17 {
- public static void main(String[] args)
- {
- int x,y,count=0;
- while(true)
- {
- if(count==3)
- {
- System.out.println("您已輸入錯誤三次了!");
- return;
- }
- try
- {
- Scanner c= new Scanner(System.in);
- System.out.print("請輸入x:");
- x=c.nextInt();
- System.out.print("請輸入y:");
- y=c.nextInt();
- System.out.print("x/y="+(x/y));
- }catch(ArithmeticException e)
- {
- count++;
- System.out.println("分母不可以為零!");
- }catch(InputMismatchException e)
- {
- count++;
- System.out.println("輸入必須為整數!");
- }
- }
- }
- }
複製代碼 |