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. What am I doing wrong? Am I missing out on something?
我试过运行这个,但打印出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);
}
}
2 个解决方案
#1
2
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.
private int circleCounter = 0;适用于全球范围。
int circleCounter = 0;
is applicable to the scope local to your method results
. The most local scope takes precedence with variables, so you've thus shadowed your global variable by redeclaring it here.
int circleCounter = 0;适用于方法结果的本地范围。最本地的范围优先于变量,因此您可以通过在此处重新声明它来遮蔽全局变量。
Simply take out that declaration and your variable won't be shadowed.
只需取出该声明,您的变量就不会被遮蔽。
Edit: This also presumes that you actually call the method, too.
编辑:这也假设您实际上也调用了该方法。
#2
2
The main in your code does not invoke the results() method and hence all the default values of the fields are printed on your console i.e either 0 or 0.0(for double) as main is the only entry point for java in your program.
代码中的main不调用results()方法,因此字段的所有默认值都打印在控制台上,即0或0.0(对于double),因为main是程序中java的唯一入口点。
#1
2
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.
private int circleCounter = 0;适用于全球范围。
int circleCounter = 0;
is applicable to the scope local to your method results
. The most local scope takes precedence with variables, so you've thus shadowed your global variable by redeclaring it here.
int circleCounter = 0;适用于方法结果的本地范围。最本地的范围优先于变量,因此您可以通过在此处重新声明它来遮蔽全局变量。
Simply take out that declaration and your variable won't be shadowed.
只需取出该声明,您的变量就不会被遮蔽。
Edit: This also presumes that you actually call the method, too.
编辑:这也假设您实际上也调用了该方法。
#2
2
The main in your code does not invoke the results() method and hence all the default values of the fields are printed on your console i.e either 0 or 0.0(for double) as main is the only entry point for java in your program.
代码中的main不调用results()方法,因此字段的所有默认值都打印在控制台上,即0或0.0(对于double),因为main是程序中java的唯一入口点。