I am having problems converting this formula V = 4/3 π r^3
. I used Math.PI
and Math.pow
, but I get this error:
';' expected
Also, the diameter variable doesn't work. Is there an error there?
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 ");
}
}
Replace
With:
Here is usage of
Math.PI
to find circumference of circle and Area First we take Radius as a string in Message Box and convert it into integerYou're missing the multiplication operator. Also, you want to do
4/3
in floating point, not integer math.Your diameter variable won't work because you're trying to store a String into a variable that will only accept a double. In order for it to work you will need to parse it
Ex: