Ik heb problemen met het converteren van deze formule V = 4/3 π r^3
. Ik heb Math.PI
en Math.pow
gebruikt, maar ik krijg deze foutmelding:
‘;’ verwacht
Ook de variabele diameter werkt niet. Is er een fout?
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 ");
}
}
Antwoord 1, autoriteit 100%
Je mist de vermenigvuldigingsoperator. U wilt ook 4/3
doen in drijvende komma, niet in gehele getallen.
volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
^^ ^
Antwoord 2, autoriteit 9%
Vervang
volume = (4 / 3) Math.PI * Math.pow(radius, 3);
Met:
volume = (4 * Math.PI * Math.pow(radius, 3)) / 3;
Antwoord 3, autoriteit 7%
Hier is het gebruik van Math.PI
om de omtrek van cirkel en gebied te vinden
Eerst nemen we Radius als een string in Message Box en zetten deze om in integer
public class circle {
public static void main(String[] args) {
// TODO code application logic here
String rad;
float radius,area,circum;
rad = JOptionPane.showInputDialog("Enter the Radius of circle:");
radius = Integer.parseInt(rad);
area = (float) (Math.PI*radius*radius);
circum = (float) (2*Math.PI*radius);
JOptionPane.showMessageDialog(null, "Area: " + area,"AREA",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "circumference: " + circum, "Circumfernce",JOptionPane.INFORMATION_MESSAGE);
}
}
Antwoord 4, autoriteit 2%
Je diametervariabele zal niet werken omdat je een string probeert op te slaan in een variabele die alleen een double accepteert. Om het te laten werken, moet je het ontleden
Bijvoorbeeld:
diameter = Double.parseDouble(JOptionPane.showInputDialog("enter the diameter of a sphere.");