- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class A {
- public static void main(String[] args) {
- int x, y, n=0;
- while(true)
- {
- try{
- if(n==3)
- {
- System.out.println("wrong");
- return;
- }
- Scanner s=new Scanner(System.in);
- System.out.println("enter numerator:");
- x=s.nextInt();
- System.out.println("enter denominator:");
- y=s.nextInt();
- System.out.println(x+"/"+y+"="+(x/y));
- }catch(ArithmeticException e){
- n++;
- System.out.println("denominator cannot be 0");
- }catch(InputMismatchException e){
- n++;
- System.out.println("enter an integer");
- }
- }
- }
- }
複製代碼 |