创建三个对象后,它应该从用户那里获取半径和高度,计算体积,然后再次询问下一个圆柱体。当我运行此代码时,它会提示我输入半径和高度,但它不会计算体积。我究竟做错了什么?
import javax.swing.JOptionPane;
import java.io.*;
public class CylinderTest
{
public static void main(String[] args) throws IOException
{
String input;
String input2;
double radius[] = new double[3];
double height[] = new double[3];
Cylinder[] myCylinder = new Cylinder[3];
for (int i = 0; i < myCylinder.length; i++)
{
input = JOptionPane.showInputDialog("Enter a radius");
radius[ i ] = Double.parseDouble(input);
input2 = JOptionPane.showInputDialog("Enter a height" +
"");
height[i] = Double.parseDouble(input2);
myCylinder[i].height = input2;
myCylinder[i].radius = input;
JOptionPane.showMessageDialog(null, " The volume of the cylinder is: " + myCylinder.getVolume());
}
}
static class Cylinder
{
private double radius;
private double height;
public Cylinder(double radius, double height)
{ this.radius = radius;
this.height = height;
}
public double getVolume()
{
return radius * radius * height * Math.PI;
}
}
}