您的位置:首页 > 移动开发 > 微信开发

Java源码-第一个异常处理小程序:整数除法

2016-07-20 22:13 441 查看
Java how to program(中文名:Java大学教程)第10版第11章:深入理解异常(Exception Handling: A Deeper Look)中给出了一个整数除法的示例,用户输入除数和被除数时,任何一个出错,就必须重新来过。考虑到这样做不太人性化,决定改写一下。

代码如下:

package exercise;

//Fig. 11.3: DivideByZeroWithExceptionHandling.java
//Handling ArithmeticExceptions and InputMismatchExceptions.
import java.util.InputMismatchException;
import java.util.Scanner;

public class FirstExceptionHandler
{
//demonstrates throwing an exception when a divide-by-zero occurs
public static int quotient(int numerator, int denominator)
throws ArithmeticException
{
return numerator / denominator; // possible division by zero
}

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
boolean continueLoop = true; // determines if more input is needed
int numerator=0;
int denominator=0;
int result=0;

do
{
try // 读取被除数(整数)
{
System.out.print("请输入被除数: ");
numerator = scanner.nextInt();
continueLoop = false; // 正确读取到被除数,跳出while循环
}
//如果用户输入不是整数,则抛出inputMismatchException(输入不匹配异常)
catch (InputMismatchException inputMismatchException)
{
System.err.printf("%n发生异常:%s%n",
inputMismatchException);
System.out.printf(
"输入非法,请输入一个整数:%n%n");
scanner.nextLine(); // discard input so user can try again

}
} while (continueLoop);

continueLoop = true;//重置循环标记
do
{
try // 读取除数(非零整数)
{
System.out.print("请输入除数:");
denominator = scanner.nextInt();
result = quotient(numerator, denominator);
System.out.printf("%n计算结果: %d除以%d = %d%n", numerator,
denominator, result);
continueLoop = false; // input successful; end looping
}
catch (InputMismatchException inputMismatchException)
{
System.err.printf("%n发生异常:%s%n",
inputMismatchException);
scanner.nextLine(); // discard input so user can try again
System.out.printf(
"输入非法,请输入一个整数:%n%n");
}
catch (ArithmeticException arithmeticException)
{
System.err.printf("%n发生异常:%s%n", arithmeticException);
System.out.printf(
"整数除法中,除数不能为零,请重新输入!%n%n");
}
} while (continueLoop);
}
} // end class DivideByZeroWithExceptionHandling


运行结果:

请输入被除数: a

发生异常:java.util.InputMismatchException

输入非法,请输入一个整数:

请输入被除数: 11.1

发生异常:java.util.InputMismatchException

输入非法,请输入一个整数:

请输入被除数: 88

请输入除数:b

发生异常:java.util.InputMismatchException

输入非法,请输入一个整数:

请输入除数:0

发生异常:java.lang.ArithmeticException: / by zero

整数除法中,除数不能为零,请重新输入!

请输入除数:11

计算结果: 88除以11 = 8

参考:

原代码如下:

package example;

//Fig. 11.3: DivideByZeroWithExceptionHandling.java
//Handling ArithmeticExceptions and InputMismatchExceptions.
import java.util.InputMismatchException;
import java.util.Scanner;

public class DivideByZeroWithExceptionHandling
{
// demonstrates throwing an exception when a divide-by-zero occurs
public static int quotient(int numerator, int denominator)
throws ArithmeticException
{
return numerator / denominator; // possible division by zero
}

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
boolean continueLoop = true; // determines if more input is needed

do
{
try // read two numbers and calculate quotient
{
System.out.print("Please enter an integer numerator: ");
int numerator = scanner.nextInt();
System.out.print("Please enter an integer denominator: ");
int denominator = scanner.nextInt();

int result = quotient(numerator, denominator);
System.out.printf("%nResult: %d / %d = %d%n", numerator,
denominator, result);
continueLoop = false; // input successful; end looping
}
catch (InputMismatchException inputMismatchException)
{
System.err.printf("%nException: %s%n",
inputMismatchException);
scanner.nextLine(); // discard input so user can try again
System.out.printf(
"You must enter integers. Please try again.%n%n");
}
catch (ArithmeticException arithmeticException)
{
System.err.printf("%nException: %s%n", arithmeticException);
System.out.printf(
"Zero is an invalid denominator. Please try again.%n%n");
}
} while (continueLoop);
}
} // end class DivideByZeroWithExceptionHandling

备注:修改后虽然对用户更友好的同时,代码也变长了,突然理解了作者为什么没有写成我那样了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: