0

从字面上看,我的程序正在运行,即将上交,然后为了安全起见再次编译它,这个错误?

我迫切需要一些快速帮助,因为我完全不知道为什么我的代码突然决定不再工作了:(((((((((((((((()))

TrominoSolver.java:175: error: constructor tromino in class tromino cannot be applied to given types;
            tromino thisguy = new tromino(size, x, y);
                              ^
  required: no arguments
  found: int,int,int
  reason: actual and formal argument lists differ in length

为什么我会突然得到这个?

这是我的代码

 import java.util.*;

public class TrominoSolver {
    //create a drawing panel of width=400px and height=400px

    private int[][] board;
    private int currentNum;

    // 0<=x<size, 0<=y<size
    // create an empty tromino object of dimension size x size.
    public void tromino(int size, int x, int y) {

        int actualsize = 1;
        while (actualsize < size) actualsize*=2;     //actualsize = actualsize * 2

        //Board is power of 2
        board = new int[actualsize][actualsize];
        currentNum = 1;

        // Initialize with empty squares.
        for (int i=0; i<actualsize; i++) {
            for (int j=0; j<actualsize; j++) {
                board[i][j] = 0;
            }
        }

        // Hole in board
        board[x][y] = -1;
    }

    // call for recursive method.
    public void tile() {
        tileRec(board.length, 0, 0);
    }

    private void tileRec(int size, int topx, int topy) {

        // fill in your one tromino...
        if (size == 2) {

            // Fill in the one tromino. The hole is identified by a
            // -num, don't fill in that one square.   
            for (int i=0; i<size; i++) 
                for (int j=0; j<size; j++)
                    if (board[topx+i][topy+j] == 0)
                        board[topx+i][topy+j] = currentNum;

            // Advance to the next tromino.
            currentNum++;
        }

        // Recursive case...
        else {

            // Find coordinates of hole
            int yesx=topx, yesy=topy;

            for (int x=topx; x<topx+size; x++) 
                for (int y=topy; y<topy+size; y++)
                    if (board[x][y] != 0) {
                        yesx = x;
                        yesy = y;
                    }

            // Hole in upper left quadrant.     
            if (yesx < topx + size/2 && yesy < topy + size/2) {

                // Recursively tile upper left quadrant.
                tileRec(size/2, topx, topy);

                // Fill in middle tromino
                board[topx+size/2][topy+size/2-1] = currentNum;
                board[topx+size/2][topy+size/2] = currentNum;
                board[topx+size/2-1][topy+size/2] = currentNum;

                // Advance to the next tromino
                currentNum++;

                // make other recursive calls.
                tileRec(size/2, topx, topy+size/2);
                tileRec(size/2, topx+size/2, topy);
                tileRec(size/2, topx+size/2, topy+size/2);

            }

            // Hole in upper right quadrant
            else if (yesx < topx + size/2 && yesy >= topy + size/2) {

                // Recursively tile upper right quadrant.
                tileRec(size/2, topx, topy+size/2);

                // Fill in middle tromino
                board[topx+size/2][topy+size/2-1] = currentNum;
                board[topx+size/2][topy+size/2] = currentNum;
                board[topx+size/2-1][topy+size/2-1] = currentNum;

                // Go to the next tromino
                currentNum++;

                // make other recursive calls.
                tileRec(size/2, topx, topy);
                tileRec(size/2, topx+size/2, topy);
                tileRec(size/2, topx+size/2, topy+size/2);

            }

            // Hole in bottom left quadrant
            else if (yesx >= topx + size/2 && yesy < topy + size/2) {

                // Recursively tile bottom left quadrant.
                tileRec(size/2, topx+size/2, topy);

                // Fill in middle tromino
                board[topx+size/2-1][topy+size/2] = currentNum;
                board[topx+size/2][topy+size/2] = currentNum;
                board[topx+size/2-1][topy+size/2-1] = currentNum;

                // Go to the next tromino
                currentNum++;

                // make other recursive calls.
                tileRec(size/2, topx, topy);
                tileRec(size/2, topx, topy+size/2);
                tileRec(size/2, topx+size/2, topy+size/2);
            }
            else {

                // Recursively tile bottom right quadrant.
                tileRec(size/2, topx+size/2, topy+size/2);

                // Fill in middle tromino
                board[topx+size/2-1][topy+size/2] = currentNum;
                board[topx+size/2][topy+size/2-1] = currentNum;
                board[topx+size/2-1][topy+size/2-1] = currentNum;

                // Go to the next tromino
                currentNum++;

                // Now we can make our three other recursive calls.
                tileRec(size/2, topx+size/2, topy);
                tileRec(size/2, topx, topy+size/2);
                tileRec(size/2, topx, topy);
            }

        } 

    } 

    // Print out latest object
    public void print() {

        for (int i=0; i<board.length; i++) {
            for (int j=0; j<board[i].length; j++)
                System.out.print(board[i][j] + "\t");
            System.out.println();
        }
    }
    public static void main(String[] args) {

        Scanner stdin = new Scanner(System.in);

        // user input...
        int size = stdin.nextInt();
        int x = stdin.nextInt();
        int y = stdin.nextInt();

        tromino thisguy = new tromino();
        thisguy.tile();

        // Print out the tromino board.
        thisguy.print();

    }
}
4

3 回答 3

2

将构造函数名称更改为 TrominoSolver:

// 0<=x<size, 0<=y<size
// create an empty tromino object of dimension size x size.
public TrominoSolver(int size, int x, int y) {

    int actualsize = 1;
    ...

更新 main 以调用正确的构造函数名称:

public static void main(String[] args) {

    Scanner stdin = new Scanner(System.in);

    // user input...
    int size = stdin.nextInt();
    int x = stdin.nextInt();
    int y = stdin.nextInt();

    TrominoSolver thisguy = new TrominoSolver();
    thisguy.tile();

    // Print out the tromino board.
    thisguy.print();

}
于 2013-11-12T08:24:49.280 回答
0

构造函数声明看起来像方法声明——除了它们使用类的名称并且没有返回类型,所以public void tromino(int size, int x, int y) {你应该写而不是写public TrominoSolver(int size, int x, int y) {,然后你可以用这种方式实例化你的类: TrominoSolver thisguy = new TrominoSolver(size, x, y);

于 2013-11-12T08:13:04.253 回答
0

错误是因为

tromino不是你的构造函数,它是你的方法。构造函数没有返回类型。

您可以使用无参数构造函数(默认提供),也可以显式声明一个或多个带有参数的构造函数。

你可以用这样的论点声明一个构造函数

public TrominoSolver(int size,int x,int y) {
  this.size=size;
  this.x=x;
  this.y=y;
}

提供size , x and y的是您TrominoSolver班级的成员。

你可以像这样使用这个构造函数创建一个对象

TrominoSolver trominoSolver = new TrominoSolver(3,2,1);

trominoSolver使用参考调用您的方法。

于 2013-11-12T08:20:39.897 回答