问题描述
- 怎样实现这个异常的循环?求大神
- package ch06;
import java.util.InputMismatchException;
import java.util.Scanner;public class DivideDemo {
/** * @param args */public static void main(String[] args) { // TODO Auto-generated method stub try{ Scanner sc=new Scanner(System.in); System.out.println(""请输入被除数:""); int a=sc.nextInt(); System.out.println(""请输入除数:""); int b=sc.nextInt(); int c=a/b; System.out.println(""两者之商是:""+c); System.out.println(""我在学习异常!""); }catch(InputMismatchException e){ System.out.println(""请输入整数!""); }catch(ArithmeticException e){ System.out.println(""除数不可以为0!""); }catch(Exception e){ System.out.println(""其他异常!""); }finally{ System.out.println(""我学会异常了!""); } System.out.println(""我不再finally里面!"");}
}
解决方案
Boolean b = false;do {try{ Scanner sc=new Scanner(System.in); System.out.println(""请输入被除数:""); int a=sc.nextInt(); System.out.println(""请输入除数:""); int b=sc.nextInt(); int c=a/b; System.out.println(""两者之商是:""+c); System.out.println(""我在学习异常!""); b = true; }catch(InputMismatchException e){ System.out.println(""请输入整数!""); }catch(ArithmeticException e){ System.out.println(""除数不可以为0!""); }catch(Exception e){ System.out.println(""其他异常!""); }finally{ System.out.println(""我学会异常了!""); } System.out.println(""我不再finally里面!"");} while (!b);
时间: 2025-01-30 14:36:35