废话不多说,直接上代码。
import java.util.Scanner; public class ArithmaticException { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("===== 计算开始 ====="); try { System.out.println("请输入除数:"); int x=scan.nextInt(); System.out.println("请输入被除数:"); int y=scan.nextInt(); double result = x/y ; System.out.println(x+"/"+y+"="+result); } catch (ArithmeticException e) { System.out.println("输入非法!除数不能为0!"); } finally { System.out.println("===== 计算结束 ====="); } } }
Java编写程序,从命令行输入两个整数,求他们的商。。要求党除数为零时,捕捉ArithmaticException异常。
最新推荐文章于 2023-10-11 09:53:04 发布