我迷失了,我甚至不知道要问什么问题。我有这个程序,应该能够构造一个参数半径和高度的圆柱体。然后它应该能够调用各种方法来获取和设置半径以及输出表面积和体积。我无法通过去,因为我不能在我的主要内容中添加任何内容而没有关于静态中使用的静态非静态方法的错误。我甚至都不知道这意味着什么。我实际上将其他人的代码复制到我的编译器中,它给了我同样的错误。我搞砸了一些设置吗?我知道这对于Stack Overflow来说可能太基础了,但我现在很绝望。
public class Miller_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1,17);
Cylinder cylinder2 = new Cylinder(3,8);
Cylinder cylinder3 = new Cylinder(2,12);
Cylinder cylinder4 = new Cylinder (1,14);
}
public class Cylinder{
private double radius = 0.0;
private double height= 0.0;
private double area = 0.0;
private double volume=0.0;
private String shape = "cylinder";
public Cylinder(double r,double h){
this.radius = r;
System.out.print(r);
this.height = h;
System.out.print(h);
}
public double getVolume(){
double volume = 3.14 * radius * radius * height;
return volume;
}
public double getArea(){
double circumference = 3.14 * 2 * radius;
double circleArea = 3.14 * radius * radius;
double area = (2 * circleArea) + (circumference * this.height);
return area;
}
public double getRadius(){
return this.radius;
}
public double getHeight(){
return this.height;
}
public void setHeight(double h){
this.height = h;
}
public void setRadius(double r){
this.radius = r;
}
@Override
public String toString(){
return this.shape + this.radius + this.height+ this.volume + this.area;
}
}
}
答案 0 :(得分:5)
内部类与任何其他成员一样(嗯,enum
除外)。如果您没有明确声明它们static
,那么它们将不会成为,因此您无法从静态上下文(例如main
)访问它们。长话短说 - 将Cylinder
内部类声明为static
,你应该没问题:
public class Miller_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1,17);
Cylinder cylinder2 = new Cylinder(3,8);
Cylinder cylinder3 = new Cylinder(2,12);
Cylinder cylinder4 = new Cylinder (1,14);
}
public static class Cylinder{
// etc...
答案 1 :(得分:2)
我不知道你是否需要外类,但是如果你把main方法放在Cylinder类中,它就会为我编译....
public class Cylinder {
private double radius = 0.0;
private double height = 0.0;
private double area = 0.0;
private double volume = 0.0;
private String shape = "cylinder";
public Cylinder(double r, double h) {
this.radius = r;
System.out.print(r);
this.height = h;
System.out.print(h);
}
public double getVolume() {
double volume = 3.14 * radius * radius * height;
return volume;
}
public double getArea() {
double circumference = 3.14 * 2 * radius;
double circleArea = 3.14 * radius * radius;
double area = (2 * circleArea) + (circumference * this.height);
return area;
}
public double getRadius() {
return this.radius;
}
public double getHeight() {
return this.height;
}
public void setHeight(double h) {
this.height = h;
}
public void setRadius(double r) {
this.radius = r;
}
@Override
public String toString() {
return this.shape + this.radius + this.height + this.volume + this.area;
}
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1, 17);
Cylinder cylinder2 = new Cylinder(3, 8);
Cylinder cylinder3 = new Cylinder(2, 12);
Cylinder cylinder4 = new Cylinder(1, 14);
}
}