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