-1

我试过运行它,但打印出来的circleCounter只打印 0。如果我将计数器代码放在底部的测试器功能下,那么它会起作用。我究竟做错了什么?我错过了什么吗?

public class Project1 {
    public int circleCounter; // Number of non-singular circles in the file.
    public int posFirstLast;  // Indicates whether the first and last circles overlap or not.
    public double maxArea;       // Area of the largest circle (by area).
    public double minArea;       // Area of the smallest circle (by area).
    public double averageArea;   // Average area of the circles.
    public double stdArea;       // Standard deviation of area of the circles.
    public double medArea;       // Median of the area.
    public int stamp = 189375;

    public Project1() {
        // This method is complete.
    }


    public void results(String fileName) {
        MaInput F1 = new MaInput("DataFile.data");
        double x, y, rad;
        int circleCounter = 0;
        double sumArea = 0;
        Circle A = new Circle();

        while (!F1.atEOF()) {
            x = F1.readDouble();
            y = F1.readDouble();
            rad = F1.readDouble();

            circleCounter++;

            if (A.area() > maxArea) {
                maxArea = A.area();
            }

            if (A.area() < minArea) {
                minArea = A.area();
            }

            sumArea += A.area();
            averageArea = sumArea / circleCounter;

            stdArea = Math.sqrt((Math.pow(A.area() - averageArea, 2) / circleCounter));

            //Array for points
            Circle[] points = new Circle[circleCounter];
            for (int j = 0; j < points.length; j++) {
                if (rad > Point.GEOMTOL) {
                    points[j] = A;
                }
            }

            posFirstLast = points[1].overlap(points[points.length]);

            //Array of areas
            double[] areas = new double[circleCounter];
            for (int i = 0; i < areas.length; i++) {
                if (rad > Point.GEOMTOL) {
                    areas[i] = A.area();
                }
            }

            //Bubble Sort
            for (int i = 0; i < areas.length; i++) {
                if (areas[i + 1] < areas[i]) {
                    double temp = areas[i + 1];
                    areas[i + 1] = areas[i];
                    areas[i] = temp;
                }
            }

            //Median
            if (areas.length % 2 == 0) {
                medArea = (0 / 5) * (areas[(areas.length / 2) - 1] + areas[areas.length / 2]);
            } else {
                medArea = (0.5) * (areas[((areas.length) - 1) / 2]);
            }

        }
    }

    public static void main(String args[]) {

        Project1 pleasework = new Project1();
        System.out.println("Number of (non-singular) circles: " + pleasework.circleCounter);
        System.out.println("Whether the first and last circles overlap: " + pleasework.posFirstLast);
        System.out.println("Maximum Area: " + pleasework.maxArea);
        System.out.println("Minimum Area: " + pleasework.minArea);
        System.out.println("Average Area: " + pleasework.averageArea);
        System.out.println("Standard deviation of the areas: " + pleasework.stdArea);
        System.out.println("Median of the areas: " + pleasework.medArea);
    }
}
4

2 回答 2

2

所以,如果只有circleCounter仍然给你 0,那么你应该注意隐藏你的变量。

private int circleCounter = 0;适用于全局范围。

int circleCounter = 0;适用于您方法的本地范围results。最局部的范围优先于变量,因此您通过在此处重新声明全局变量来隐藏全局变量。

只需取出该声明,您的变量就不会被遮蔽。

编辑:这也假定您实际上也调用了该方法。

于 2013-02-24T18:37:46.080 回答
2

代码中的 main 不调用 results() 方法,因此所有字段的默认值都打印在控制台上,即 0 或 0.0(对于双精度),因为 main 是程序中 java 的唯一入口点。

于 2013-02-24T18:38:44.647 回答