好的,现在我已经更新了我的代码,它看起来像这样:
public static double getDouble (String userShape, String parameter) throws BadShapeData
{
String missingValue = parameter, value = "", shape = userShape;
String s2 = "Enter a value for " + missingValue;
value = JOptionPane.showInputDialog(s2);
if (null == value || value.length() == 0) {
throw new BadShapeData("Error, nothing was entered. Must be double.");
}
try {
return Double.parseDouble(value);
}
catch (NumberFormatException e) {
throw new BadShapeData("Error entering " + value + ". Must be double.");
}
}
这也在我的代码中:
public static void main(String args[]) {
int choice;
do {
choice = menu();
if(choice != 0) {
System.out.println(makeShape(choice));
}
} while (choice != 0);
}
public static Shape3D makeShape(int choice) {
if(choice == 1)
return new Cone(getDouble("Cone", "Radius"), getDouble("Cone", "Height"));
else if(choice == 2)
return new Cylinder(getDouble("Cylinder", "Radius"), getDouble("Cylinder", "Height"));
else if(choice == 3)
return new Sphere(getDouble("Sphere", "Radius"));
else if(choice == 4)
return new Box(getDouble("Box", "Length"), getDouble("Box", "Width"), getDouble("Box", "Height"));
else if(choice == 5) return new Pyramid(getDouble("Pyramid", "Base"), getDouble("Pyramid", "Height"));
else return new Cube(getDouble("Cube", "Size"));
}
但是现在我收到的错误是“错误:未报告的异常 BadShapeData;必须被捕获或声明为抛出”,并且在我使用 getDouble 方法的地方突出显示