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