- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Ch01
- {
- static Scanner s=new Scanner(System.in);
- public static void main(String[] args)
- {
- int x,y;
- int n=0; //次數
- while(true)
- {
- try
- {
- n++; //次數+1
-
- System.out.print("輸入x: ");
- x=s.nextInt();
- System.out.print("輸入y: ");
- y=s.nextInt();
- System.out.println(x+"+"+y+"="+(x+y));
- System.out.println(x+"-"+y+"="+(x-y));
- System.out.println(x+"*"+y+"="+(x*y));
- System.out.println(x+"/"+y+"="+(x/y));
- System.out.println(x+"%"+y+"="+(x%y));
-
- n++;
- if(n==5)
- {
- //次數=5,跳出(結束迴圈)
- break;
- }
- }
- catch(ArithmeticException e)
- {
- System.out.println("分母不能為0");
- s.next(); //清快取
- }
- catch(InputMismatchException e)
- {
- System.out.println("輸入非int");
- s.next(); //清快取
- }
- catch(Exception e)
- {
- System.out.println(e);
- s.next(); //清快取
- }
- }
-
-
-
- }
- }
複製代碼 |