我在转换此公式时遇到问题V = 4/3 π r^3
。我用了Math.PI
and Math.pow
,但我收到此错误:
';'预期的
此外,直径变量不起作用。那里有错误吗?
import java.util.Scanner;
import javax.swing.JOptionPane;
public class NumericTypes
{
public static void main (String [] args)
{
double radius;
double volume;
double diameter;
diameter = JOptionPane.showInputDialog("enter the diameter of a sphere.");
radius = diameter / 2;
volume = (4 / 3) Math.PI * Math.pow(radius, 3);
JOptionPane.showMessageDialog("The radius for the sphere is "+ radius
+ "and the volume of the sphere is ");
}
}
您缺少乘法运算符。另外,你想做4/3
在浮点数中,而不是整数数学中。
volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
^^ ^
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)