当我编译以下java代码时,它会抛出illegal start of type
& class, interface or enum expected
错误。
在以下代码中的Percolation类中,声明了属于另一个类的WeightedQuickUnionUF数据类型。 WeightedQuickUnionUF类存储在名为" algs4.jar"的jar文件中。在当前工作目录中。
我已多次检查过代码,但无法找到该漏洞并完全陷入困境。
代码如下:
public class Percolation{
public static Percolation(int n){
boolean[][] A=new boolean[n][n];
}
int i,j;
WeightedQuickUnionUF WQUF=new WeightedQuickUnionUF[n*n+2];
for(i=1;i<=n;i++)
{
WQUF.union(0,i);
}
for (i=n*n-n+1;i<=n*n;i++)
{
WQUF.union(n*n+1,i);
}
do {
i=StdRandom.uniform(n-1)+1;
j=StdRandom.uniform(n-1)+1;
} while(A[i][j]!=0)
open(i,j);
private static int xyto1D(int x,int y){
return (n*x+y);
}
public static void open(int i,int j){
A[i][j]=1;
if(A[i-1][j]==1) {WQUF.union(xyto1D(i-1,j),xyto1D(i,j))};
if(A[i+1][j]==1) {WQUF.union(xyto1D(i+1,j),xyto1D(i,j))};
if(A[i][j-1]==1) {WQUF.union(xyto1D(i,j-1),xyto1D(i,j))};
if(A[i][j+1]==1) {WQUF.union(xyto1D(i,j+1),xyto1D(i,j))};
}
public static boolean isOpen(int x,int y){
return (A[x][y]==1);
}
public static boolean isFull(int x,int y){
return (WQUF.connected(xyto1D(x,y),0));
}
public static boolean percolates(){
for (i=n*n-n+1;i<=n*n;i++)
{
if ((isFull(i))&&(isOpen(i))) return TRUE;
}
return FALSE;
}
}
我在编译期间遇到以下错误:
35 errors found:
File: C:\Users\Administrator\algs4\Percolation.java
[line: 7]
Error: illegal start of type
File: C:\Users\Administrator\algs4\Percolation.java
[line: 7]
Error: <identifier> expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 7]
Error: ';' expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 7]
Error: <identifier> expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 7]
Error: <identifier> expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 7]
Error: <identifier> expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 7]
Error: illegal start of type
File: C:\Users\Administrator\algs4\Percolation.java
[line: 7]
Error: <identifier> expected
File: C:\Users\Administrator\algs4\Percolati
on.java
[line: 8]
Error: ';' expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 9]
Error: illegal start of type
File: C:\Users\Administrator\algs4\Percolation.java
[line: 9]
Error: illegal start of type
File: C:\Users\Administrator\algs4\Percolation.java
[line: 9]
Error: <identifier> expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 11]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 11]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 11]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 14]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 17]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 18]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 20]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 22]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 23]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 25]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 26]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 27]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 28]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 29]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 30]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 32]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 33]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 35]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 36]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 37]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 37]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 40]
Error: class, interface, or enum expected
File: C:\Users\Administrator\algs4\Percolation.java
[line: 42]
Error: class, interface, or enum expected
答案 0 :(得分:2)
您在第四行关闭构造函数,每个错误都是由此造成的。
public static Percolation(int n){
boolean[][] A=new boolean[n][n];
} // <<--- here
请处理你的缩进。如果我没记错的话,eclipse和netbeans都有某种自动缩进。你可以通过适当的缩进轻松地抓住它。
答案 1 :(得分:1)
此处还有一个问题:(除了许多其他语法错误之外,您还要逐行调试,这样您就会知道违反Java语法规则的地方和方法)
WeightedQuickUnionUF WQUF=new WeightedQuickUnionUF[n*n+2];
您应将其更改为:
WeightedQuickUnionUF WQUF=new WeightedQuickUnionUF(n*n+2);
答案 2 :(得分:1)
do {
i=StdRandom.uniform(n-1)+1;
j=StdRandom.uniform(n-1)+1;
} while(A[i][j]!=0)
您需要将分号放在while语句的末尾。
do {
i=StdRandom.uniform(n-1)+1;
j=StdRandom.uniform(n-1)+1;
} while(A[i][j]!=0);
代码似乎有很多问题。我建议你使用Eclipse IDE。
答案 3 :(得分:1)
在Java中,每个可执行代码(初始化除外)都需要放在一个块(静态或函数)中。
然而,在你的文件的第7行,你有一个for
循环,它在类的主体中,这是无效的。请把它放在构造函数或其他函数中。
事实上,您发布的代码还有一个问题。您已声明构造函数static,这是不允许的。构造函数不能是静态的。
public static Percolation(int n) { // illegal
boolean[][] A = new boolean[n][n];
}
假设从第7行开始的代码是构造函数的一部分,这里是代码,它应该工作
public class Percolation {
public Percolation(int n) {
boolean[][] A = new boolean[n][n];
int i, j;
WeightedQuickUnionUF WQUF = new WeightedQuickUnionUF[n * n + 2];
for(i=1;i<=n;i++){
WQUF.union(0,i);
}
for (i=n*n-n+1;i<=n*n;i++){
WQUF.union(n*n+1,i);
}
do{
i=StdRandom.uniform(n-1)+1;
j=StdRandom.uniform(n-1)+1;
} while(A[i][j]!=0); //add this semicolon as well.
open(i,j);
}
private static int xyto1D(int x, int y) {
return (n * x + y);
}
public static void open(int i,int j){
A[i][j]=1;
if(A[i-1][j]==1) {WQUF.union(xyto1D(i-1,j),xyto1D(i,j))};
if(A[i+1][j]==1) {WQUF.union(xyto1D(i+1,j),xyto1D(i,j))};
if(A[i][j-1]==1) {WQUF.union(xyto1D(i,j-1),xyto1D(i,j))};
if(A[i][j+1]==1) {WQUF.union(xyto1D(i,j+1),xyto1D(i,j))};
}
public static boolean isOpen(int x, int y) {
return (A[x][y] == 1);
}
public static boolean isFull(int x, int y) {
return (WQUF.connected(xyto1D(x, y), 0));
}
public static boolean percolates() {
for (i = n * n - n + 1; i <= n * n; i++) {
if ((isFull(i)) && (isOpen(i)))
return TRUE;
}
return FALSE;
}
}