这是我的java代码(我知道的一种可怕的暴力算法,这是要求)。我想我在任何引用之前已经在for循环中初始化了j
变量,但是当我运行编译器警报时。
import java.util.Arrays;
public class Brute {
public static void main(String[] args) {
String filename = "./collinear/input" + args[0] + ".txt";
In f = new In(filename);
int N = f.readInt();
Point[] points = new Point[N];
int x, y;
StdDraw.setScale(-10000, 50000);
for(int i = 0; i < N; i++) {
x = f.readInt();
y = f.readInt();
points[i] = new Point(x, y);
points[i].draw();
}
Arrays.sort(points);
int i, j, k, l;
for(i = 0; i < N; i++)
for(j = 0; j < N; j++)
if(points[j] == points[i]) continue;
for(k = 0; k < N; k++)
if((points[k] == points[i]) || (points[k] == points[j])) continue;
for(l = 0; l < N; l++) {
if((points[l] == points[i]) || (points[l] == points[j])
|| (points[l] == points[k])) continue;
if(points[i].slopeTo(points[j]) == points[i].slopeTo(points[k])
&& points[i].slopeTo(points[k]) == points[i].slopeTo(points[l])) {
StdOut.println(points[i].toString()
+ " -> " + points[j].toString()
+ " -> " + points[k].toString()
+ " -> " + points[l].toString());
points[i].drawTo(points[l]);
}
}
}
}
Brute.java:31:错误:变量j可能尚未初始化
if((points[k] == points[i]) || (points[k] == points[j])) continue;
Brute.java:33:错误:变量j可能尚未初始化
if((points[l] == points[i]) || (points[l] == points[j])
答案 0 :(得分:7)
在for循环的主体周围放置大括号。
此:
for(j = 0; j < N; j++)
if(points[j] == points[i]) continue;
for(k = 0; k < N; k++) .....
是一行的for循环,没有做任何事情,接着是另一行for循环。
你的意思是:
for(j = 0; j < N; j++) {
if(points[j] == points[i]) continue;
for(k = 0; k < N; k++) {
.....
}
}