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