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

时间:2015-02-09 05:18:43

标签: java variables non-static

我知道这些类型的问题经常被问到,我已经读过了 this onethis one 但由于某种原因,我仍然遇到麻烦,了解我目前的计划所遇到的问题。

我正在尝试创建一组类,这些类定义了一系列可以存储其大小的三维形状,并提供了更改数据的访问权限。它还应该能够计算周长,面积和体积。我只是到了我正在做的第一个选择的形状(一个球体),我在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;
    }
}

任何帮助或指导都将不胜感激。

2 个答案:

答案 0 :(得分:1)

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,它们是计算字段,您只需调用获取计算结果所需的方法即可。< / p>

字段circumferenceareavolumeradius称为实例字段,它们需要该类的实例才能使用它们。这意味着您可以拥有多个Sphere个实例,每个实例都有自己的unquie值

您可能希望仔细查看Classes and Objects

答案 1 :(得分:0)

我稍微更改了您的代码,现在它正在运行(可能不是最有效的方式):

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;
        }
    }
}