ex2.java

来自「我在学习JAVA的讲义」· Java 代码 · 共 31 行

JAVA
31
字号
class Example{
	static int result(int x, int y) throws ArithmeticException,Exception{
		int a=0, b=0, c=0;
		a=4*x-4;
		b=2*x*x-4*x*y+y*y;
		if(a==0 || b==0){
			throw new ArithmeticException("A 或 B =0");
		}
		else if (a!=0 && b!=0){
			throw new Exception("program is ok!");
		}
		c=a/b;
		return c;
	}
	public static void main(String[] args){
		int x=(int)(Math.random()*21);
		int y=(int)(Math.random()*21);
		try{
			System.out.print("result("+x+" , " +y + " )  = ");
			System.out.println("" +result(x,y));
		}catch (ArithmeticException e){
			System.out.println(e.getMessage());
		}catch (Exception e){
			System.out.println(e.getMessage());
		}finally{
			System.out.println("program is end");
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?