如何在java中使用math.pi

Iva*_*ork 29 java pi

我在转换这个公式时遇到了问题V = 4/3 ? r^3.我用Math.PIMath.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 ");
    }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*Yaw 48

你错过了乘法运算符.此外,您希望以4/3浮点运算,而不是整数运算.

volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
           ^^      ^
Run Code Online (Sandbox Code Playgroud)