不能从 println 语句中的静态上下文引用非静态变量

non-static variable cannot be referenced from a static context in println statement

我知道这类问题经常被问到,我已经阅读过 this onethis one 但出于某种原因,我仍然无法理解我当前程序遇到的问题。

我正在尝试创建一组 类,它定义了一系列可以存储其大小的 3-D 形状,并提供了更改数据的权限。它还应该能够计算周长、面积和体积。我只是到了我第一次选择的形状(球体)的地步,我在 if 语句内的 println 中遇到了这个错误。

    import java.util.Scanner;
public class Shapes
{
    public static void main(String[] args)
    {
        String input;
        double Radius;

        Scanner scan = new Scanner(System.in);

        System.out.println("Choose a shape. 'S' for sphere, 'P' for pyramid, 'C' for cone: ");
        input = scan.next();

        if (input.equals("S"))
        {
            System.out.println("Enter radius: ");
            Radius = scan.nextDouble();
            Sphere calcCircumference;
            Sphere calcArea;
            Sphere calcVolume;
            System.out.println("The circumference is "+Sphere.circumference+", the area is "+Sphere.area+", the volume is "+Sphere.volume+".");
        }

    }
}

public class Sphere
{
    // instance variables
    protected double radius;
    protected double circumference;
    protected double area;
    protected double volume;
    Scanner scan = new Scanner(System.in);

    /**
     * Constructor for objects of class Sphere
     */
    public Sphere()
    {
        // initialise instance variables
        radius = 0;
        circumference = 0;
        area = 0;
        volume = 0;
    }

    /**
     *Gets user entered radius
     */
    public double getRadius(double Radius)
    {
        radius = radius;
        return radius;
    }

    public double calcCircumference()
    {
        circumference = 2*Math.PI*radius;
        return circumference;
    }

    public double calcArea()
    {
        area = 4*Math.PI*Math.pow(radius,2);
        return area;
    }

    public double calcVolume()
    {
        volume = (4*Math.PI*Math.pow(radius,3))/3;
        return volume;
    }
}

如有任何帮助或指导,我们将不胜感激。

System.out.println("The circumference is "+Sphere.circumference+", the area is "+Sphere.area+", the volume is "+Sphere.volume+".");

您需要创建 Sphere 的实例才能访问它的字段。

Sphere sphere = new Sphere();

然后您需要提供对象需要的信息

sphere.radius = Radius; // I'd prefer a setter method

那么你可以利用这个实例提供的信息...

System.out.println("The circumference is "+sphere.calcCircumference()+", the area is "+Sphere.calcArea()+", the volume is "+Sphere.calcVolume()+".");

无需跟踪circumferenceareavolume,它们是计算字段,您只需调用您需要的方法即可获得计算结果.

字段 circumferenceareavolumeradius 被称为实例字段,它们需要 class 的实例才能使用使用它们。这意味着你可以有多个 Sphere 的实例,每个实例都有自己的非静态值

你可能想仔细看看 Classes and Objects

我稍微修改了你的代码,现在它可以工作了(可能不是最有效的方法):

import java.util.Scanner;

public class Shapes {
    public static void main(String[] args) {
        String input;
        double Radius;

        Scanner scan = new Scanner(System.in);

        System.out
                .println("Choose a shape. 'S' for sphere, 'P' for pyramid, 'C' for cone: ");
        input = scan.next();

        if (input.equals("S")) {
            System.out.println("Enter radius: ");
            Radius = scan.nextDouble();
            Sphere calcCircumference;
            Sphere calcArea;
            Sphere calcVolume;
            System.out.println("The circumference is " + Sphere.circumference
                    + ", the area is " + Sphere.area + ", the volume is "
                    + Sphere.volume + ".");
        }

    }

    public static class Sphere {
        // instance variables
        protected double radius;
        protected static double circumference;
        protected static double area;
        protected static double volume;
        Scanner scan = new Scanner(System.in);

        /**
         * Constructor for objects of class Sphere
         */
        public Sphere() {
            // initialise instance variables
            radius = 0;
            circumference = 0;
            area = 0;
            volume = 0;
        }

        /**
         * Gets user entered radius
         */
        public double getRadius(double Radius) {
            radius = radius;
            return radius;
        }

        public double calcCircumference() {
            circumference = 2 * Math.PI * radius;
            return circumference;
        }

        public double calcArea() {
            area = 4 * Math.PI * Math.pow(radius, 2);
            return area;
        }

        public double calcVolume() {
            volume = (4 * Math.PI * Math.pow(radius, 3)) / 3;
            return volume;
        }
    }
}