-2

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

4 回答 4

2

你在第四行关闭了构造函数,每个错误都是这个的结果。

    public static Percolation(int n){
        boolean[][] A=new boolean[n][n];
    } // <<--- here

请处理您的缩进。如果我没记错的话,eclipse 和 netbeans 都有某种自动缩进。通过适当的缩进,您很容易自己抓住它。

于 2013-02-16T15:44:23.747 回答
1

问题也在这里:(除了许多其他语法错误外,您逐行调试,以便您了解您在哪里以及所有违反 Java 语法规则的地方)

WeightedQuickUnionUF WQUF=new WeightedQuickUnionUF[n*n+2];

您应该将其更改为:

WeightedQuickUnionUF WQUF=new WeightedQuickUnionUF(n*n+2);
于 2013-02-16T15:46:27.573 回答
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。

于 2013-02-16T15:47:12.457 回答
1

在 Java 中,每个可执行代码(初始化除外)都需要放在一个块(静态或函数)中。

但是,在您文件的第 7 行,您for在类的主体中有一个循环,这是无效的。请把它放在构造函数或其他函数中。

实际上,您发布的代码还有一个问题。您已经声明了不允许的构造函数静态。构造函数不能是静态的。

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;
    }
}
于 2013-02-16T15:47:42.210 回答