Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这行有多个标记,Java构造函数错误(初学者级别)。_Java_Eclipse_Constructor - Fatal编程技术网

这行有多个标记,Java构造函数错误(初学者级别)。

这行有多个标记,Java构造函数错误(初学者级别)。,java,eclipse,constructor,Java,Eclipse,Constructor,我刚开始学习java,对该语言不熟悉。这是一个在线作业,我做的乐趣和获得更多的经验,并不能找出我与构造函数行得到的多个错误。请帮忙 public class WhackAMole { public static void main(String[] args) { int score; int molesLeft; int attemptsLeft; char [][]moleGrid=new char[10][10]; int numAttempts; /

我刚开始学习java,对该语言不熟悉。这是一个在线作业,我做的乐趣和获得更多的经验,并不能找出我与构造函数行得到的多个错误。请帮忙

 public class WhackAMole {
   public static void main(String[] args) {
   int score;
   int molesLeft;
   int attemptsLeft;
   char [][]moleGrid=new char[10][10];
   int numAttempts; //is this needed 
   int gridDimensions; // is this also needed 

   /*Multiple markers at this line
- Syntax error on token "int", delete this token
- Syntax error, insert ";" to complete Statement
- Syntax error on token "int", delete this token
- numAttempts cannot be resolved to a variable
- gridDimensions cannot be resolved to a variable
- Syntax error on token "int", delete this token
- The method WhackAMole(int, int) is undefined for the type 
 WhackAMole*/
     WhackAMole(int numAttempts, int gridDimensions) {
      this.numAttempts=numAttempts ;  //error-cannot use this in static content
      this.gridDimensions=gridDimensions ; // error-cannot use this in static content

}

}

}

将构造函数移出main()方法。

我建议您做一些基本的java入门教程。不能将构造函数放在另一个方法中(它在main方法中)。要使用this.numatempts,还需要对象属性。我尝试移动代码片段以使其更有意义:

public class WhackAMole {

    // Those are attributes
    private int score;
    private int molesLeft;
    private int attemptsLeft;
    private char[][] moleGrid = new char[10][10];
    private int numAttempts; // is this needed
    private int gridDimensions; // is this also needed

    // Constructor
    public WhackAMole(int numAttempts, int gridDimensions) {
        this.numAttempts = numAttempts;
        this.gridDimensions = gridDimensions;
    }

    public void play() {
        // Game logic here
    }

    /* This Method should propably be in another class */
    public static void main(String[] args) {

        final WhackAMole wham = new WhackAMole(42, 1234567);
        wham.play();
    }
}

您在java中不允许的方法中定义方法。此外,我已将属性移动到类级别

请使用以下代码:

public class WhackAMole {

    int score;
    int molesLeft;
    int attemptsLeft;
    char[][] moleGrid = new char[10][10];
    int numAttempts; //is this needed
    int gridDimensions; // is this also needed

    WhackAMole(final int numAttempts, final int gridDimensions) {
        this.numAttempts = numAttempts; //error-cannot use this in static content
        this.gridDimensions = gridDimensions; // error-cannot use this in static content
    }

    public static void main(final String[] args) {
        WhackAMole whackAMole = new WhackAMole(30, 40);
        System.out.println("numAttempts:" + whackAMole.numAttempts + " gridDimensions:" + whackAMole.gridDimensions);
    }
}

您没有关闭主方法。在
int gridDimensions之后添加一个
}
并从下面删除1个
}
。您必须将构造函数和字段移出
main
您正在混合方法和构造函数。欢迎使用堆栈溢出!为了帮助人们回答您的问题,您需要更具体地说明错误。请你的帖子包含你从你的网站上得到的确切错误(最好使用复制+粘贴来避免转录错误),而不仅仅是构造器。他也需要移动属性。他使用构造器中的变量除了你,这个作业只是从一开始的级别类。。。我注意到你们在构造函数中使用了final int,它和int有什么不同?