Java-圆柱体程序-无法使用main
我太迷茫了,我都不知道该问什么问题了。我有一个程序,它应该能够用半径和高度来构造一个圆柱体。然后,它应该能够调用各种方法来获取和设置半径以及输出表面积和体积。我无法通过go,因为我无法在main中放入任何内容,而不会出现关于在static中使用静态非静态方法的错误。我甚至不知道那是什么意思。实际上,我将其他人的代码复制到我的编译器中,它会给我同样的错误。我是不是搞砸了什么?我知道这对于堆栈溢出来说可能太简单了,但我现在非常绝望Java-圆柱体程序-无法使用main,java,methods,compiler-errors,static-methods,Java,Methods,Compiler Errors,Static Methods,我太迷茫了,我都不知道该问什么问题了。我有一个程序,它应该能够用半径和高度来构造一个圆柱体。然后,它应该能够调用各种方法来获取和设置半径以及输出表面积和体积。我无法通过go,因为我无法在main中放入任何内容,而不会出现关于在static中使用静态非静态方法的错误。我甚至不知道那是什么意思。实际上,我将其他人的代码复制到我的编译器中,它会给我同样的错误。我是不是搞砸了什么?我知道这对于堆栈溢出来说可能太简单了,但我现在非常绝望 public class Miller_A03Q1 { p
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;
}
}
}
我不知道您是否需要外部类,但如果您只是将main方法放在它为我编译的圆柱体类中
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);
}
}
我不知道您是否需要外部类,但如果您只是将main方法放在它为我编译的圆柱体类中
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);
}
}
内部类与任何其他成员一样(除了
enum
s)。如果没有显式地将它们声明为静态的,那么它们就不会是静态的,因此您将无法从静态上下文(例如main
)中访问它们。长话短说-将您的圆柱体
内部类声明为静态
,您应该可以:
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...
内部类与任何其他成员一样(除了
enum
s)。如果没有显式地将它们声明为静态的,那么它们就不会是静态的,因此您将无法从静态上下文(例如main
)中访问它们。长话短说-将您的圆柱体
内部类声明为静态
,您应该可以:
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...
尝试
公共静态类圆柱体{
…您定义的圆柱体
需要一个Miller\u A03Q1
的实例。您还可以将Miller\u A03Q1
和圆柱体
创建为两个独立的类。尝试公共静态类圆柱体{
…你定义的圆柱体
需要一个Miller\u A03Q1
的实例。你也可以创建Miller\u A03Q1
和圆柱体
作为两个独立的类。我不认识你,但我爱你。我不能相信有一个词会让我如此悲伤。@rkstm这在你编写代码时是很正常的。小的错误是最大的麻烦:我不认识你,但我爱你。我不敢相信一个字会让我如此悲伤。@rkstm这在你写代码时是很正常的。小错误是最大的麻烦:D