Java中的初学者在这里,我正在创建一个程序,该程序将在给出半径值后计算球体的面积和体积。如果半径为< = 0,那么它应该以JOptionPane的形式显示错误消息,然后返回以询问半径,但是,我正在用while循环挣扎。我的while循环出现问题
这里是我的代码:
public static void main(String[] args) {
String volume = "V = (4(pi)r^3)/3";
String area = "V = 4(pi)r^2";
JOptionPane.showMessageDialog(null, "Formulas for a Sphere:"+"\n"+"V = (4(pi)r^3)/3"+"\n"+"A = 4(pi)r^2");
Double radius = Double.parseDouble(JOptionPane.showInputDialog("Enter Radius(cm)"));
while(radius <= 0) {
JOptionPane.showMessageDialog(null, "Please insert a valid radius.");
}
JOptionPane.showMessageDialog(null, volume + " = " + Math.round((4*3.1415*Math.pow(radius,3)/3)) + "cm" +
"\n" + area + " = " + Math.round(4*3.1415*Math.pow(radius,2)));
您需要输入_inside_您的循环。否则,循环将如何结束? – khelwood