- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Ed
- {
- public static void main(String[] args)
- {
- int n=0;
- while(true)
- {
- if(n==3)a
- {
- System.out.println("錯誤嘗試過多,程式跳出!");
- return;
- }
- try
- {
-
- Scanner s=new Scanner(System.in);
- int x,y;
- System.out.print("輸入分子: ");
- x=s.nextInt();
- System.out.print("輸入分母: ");
- y=s.nextInt();
- System.out.println(x+"/"+y+"="+(x/y));
- }catch(ArithmeticException e)
- {
- System.out.println("運算錯誤!分母不可為0");
- n++;
- }catch(InputMismatchException e)
- {
- System.out.println("程式錯誤!請輸入整數!");
- n++;
- }
- }
- }
- }
複製代碼 |