简体   繁体   English

为什么我的柜台不工作? 爪哇

[英]Why doesn't my counter work? Java

I've tried running this, but printing out circleCounter only prints 0. If I were to put the counter code under the tester function in the bottom part, then it would work. 我试过运行此命令,但是打印出circleCounter只会打印0。如果我将计数器代码放在底部的tester函数下,那么它将起作用。 What am I doing wrong? 我究竟做错了什么? Am I missing out on something? 我错过了什么吗?

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

So, if it's only your circleCounter that's still giving you 0, then you should be aware of shadowing your variables. 所以,如果这只是你的circleCounter这仍然给你0,那么你应该知道你的阴影变量。

private int circleCounter = 0; is applicable to the global scope. 适用于全球范围。

int circleCounter = 0; is applicable to the scope local to your method results . 适用于方法results局部的范围。 The most local scope takes precedence with variables, so you've thus shadowed your global variable by redeclaring it here. 最局部的作用域优先于变量,因此您在这里重新声明了全局变量,从而使它成为阴影。

Simply take out that declaration and your variable won't be shadowed. 只需删除该声明,您的变量就不会被遮盖。

Edit: This also presumes that you actually call the method, too. 编辑:这也假定您实际上也调用了该方法。

代码中的main不会调用result()方法,因此,所有字段的默认值都将打印在控制台上,即0或0.0(对于double而言),因为main是程序中Java的唯一入口点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM