当我编译以下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