package com.main;
import java.util.Scanner;
class MyException extends Exception {
public MyException(String s) {
super(s);
}
}
class Math {
float div(float a, float b) throws MyException {
if (a < 0 || b < 0) {
try {
throw new MyException("输入的数为负数!");
}
catch(MyException e) {
throw e;
}
finally {
System.out.println("输入结束.");
}
}
if (b == 0) {
try {
throw new MyException("除数为零.");
}
catch(MyException e) {
throw e;
}
finally {
System.out.println("输入结束.");
}
}
return a / b;
}
}
class Text {
public static void main(String[] args) {
Math m = new Math();
Scanner s = new Scanner(System.in);
System.out.println("两个数的除法(大于零):");
while (s.hasNextFloat()) {
float a, b;
a = s.nextFloat();
b = s.nextFloat();
try {
System.out.println(m.div(a, b));
}
catch(Exception e) {
System.out.println(e);
System.out.println();
}
}
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容