哪位好心人JAVA编程题可以帮忙写一下?
①
public class DividedByZeroException extends Exception {
private static final long serialVersionUID = 4420887449631055240L;
public String getMessage() {
return "除数不能为零";
}
}
上面是异常类DividedByZeroException的代码。
②
import java.util.Scanner;
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
System.out.print("请输入被除数:");
Scanner scanner1 = new Scanner(System.in);
int input1 = scanner1.nextInt();
System.out.print("请输入除数:");
Scanner scanner2 = new Scanner(System.in);
int input2 = scanner2.nextInt();
System.out.print("两个数相除结果为:");
if (input2 == 0) {
try {
throw new DividedByZeroException();
} catch (DividedByZeroException e) {
e.printStackTrace();
}
} else {
System.out.println(input1 / input2);
}
scanner1.close();
scanner2.close();
}
}
上面是测试类代码和运行结果,麻烦看一下是否能满足要求。