首页 > 编程知识 正文

两个数相除异常处理 Java,java两个整数相除

时间:2023-05-06 14:02:13 阅读:209667 作者:3178

package chapter12;import java.util.InputMismatchException;import java.util.Scanner;public class FinallyTest1 { public static void main(Stringdfdsl args) { Scanner input = new Scanner(System.in); boolean ok = true; boolean yes = true; int num1 = 1; int num2 = 1; do { System.out.println("please enter two number :"); try { num1 = input.nextInt(); num2 = input.nextInt(); ok = false; } catch (InputMismatchException e) { System.out.println(e.getMessage()); System.out.println("enter again "); input.nextLine(); } } while (ok); do { try { int result = num1 / num2; System.out.println("num1 / num2 is " + result); yes = false; } catch (ArithmeticException e) { System.out.println(e.getMessage()); boolean in = true; do{ System.out.println("enter num2 again "); try { num2 = input.nextInt(); in = false; } catch(ArithmeticException e1) { System.out.println(e1.getMessage()); input.nextLine(); } } while(in); } } while (yes); }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。