我有一个家庭作业,我从用户那里获取圆柱体的半径和高度,然后返回体积。我认为我已经完成了大部分工作,但是很难将它们整合在一起。当我尝试将其全部打印出来时出现语法错误。这是打印线
for (int i = 0; i < volume.length; i++)
{
System.out.println("for Radius of:" + volume[i].getRadius() + " and Height of:" + volume[i].getHeight() + " the Volume is:" + volume.getVolume());
}
这是主要课程
import javax.swing.*;
//Driver class
public class CylinderTest
{
public static void main(String[] args)
{
Cylinder[] volume = new Cylinder[3];
for (int counter = 0; counter < volume.length; counter++)
{
double radius = Double.parseDouble(JOptionPane
.showInputDialog("Enter the radius"));
double height = Double.parseDouble(JOptionPane
.showInputDialog("Enter the height"));
volume[counter] = new Cylinder(radius, height);
}
for (int i = 0; i < volume.length; i++)
{
System.out.println("for Radius of:" + volume[i].getRadius() + " and Height of:" + volume[i].getHeight() + " the Volume is:" + volume.getVolume());
}
}
}
这是气缸类
public class Cylinder
{
// variables
public static final double PI = 3.14159;
private double radius, height, volume;
// constructor
public Cylinder(double radius, double height)
{
this.radius = radius;
this.height = height;
this.volume = volume;
}
// default constructor
public Cylinder()
{this(0, 0);}
// accessors and mutators (getters and setters)
public double getRadius()
{return radius;}
private void setRadius(double radius)
{this.radius = radius;}
public double getHeight()
{return height;}
private void setHeight(double height)
{this.height = height;}
public double getVolume()
{return volume;}
// Volume method to compute the volume of the cylinder
public double volume()
{return PI * radius * radius * height;}
public String toString()
{return volume + "\t" + radius + "\t" + height; }
}
我正在尝试从 Cylinder 类 getVolume getter 中获取音量。