- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class A02 {
- public static void main(String[] args)
- {
- int a, b, s=0;
- while(true)
- {
- if(s==3)
- {
- System.out.print("嘗試次數過多");
- return;
- }
- try
- {
- Scanner c=new Scanner(System.in);
- System.out.print("請輸入分子: ");
- a=c.nextInt();
- System.out.print("請輸入分母: ");
- b=c.nextInt();
- System.out.print(a+"/"+b+"="+(a/b));
- }catch(ArithmeticException e)
- {
- s++;
- System.out.println("分母不可為零");
- }catch(InputMismatchException e)
- {
- s++;
- System.out.println("請輸入整數");
- }
- }
- }
- }
複製代碼 |