- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Hello {
- public static void main(String[] args) {
- // TODO 自動產生的方法 Stub
- int x ,y ,n=0;
- for(;;)
- {
- if(n==3)
- {
- System.out.println("錯誤過多");
- return;
- }
- try
- {
- Scanner s=new Scanner(System.in);
- System.out.print("請輸入x:");
- x=s.nextInt();
- System.out.print("請輸入y:");
- y=s.nextInt();
- System.out.println("x/y="+(x/y));
- }catch(ArithmeticException e)
- {
- n++;
- System.out.println("運算錯誤! 分母不可為零!");
- }catch(InputMismatchException e)
- {
- n++;
- System.out.println("格式錯誤! 輸入須為整數!");
- }
- }
- }
- }
複製代碼 |