无法在构造函数中分配Java属性



我正试图将一个值传递给我在Java中创建的类的一个属性,但它根本不起作用。

这是我的代码:

// Landscape Class
public class Landscape{
// Attribute
char [] [] landscape ;

// Constructor to create the landscape 
public void Landscape(){
landscape = {
{'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
{'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
{'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
{'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
{'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
{'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
{'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
{'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'}

};
}
// Function to display the landscape
public void display(){
System.out.println("");
for(char [] element : landscape){

System.out.print("              ");

for(char character : element){
System.out.print(character);
}

System.out.println("");

}
System.out.println("");
}
public static void main(String[] args){

Landscape landscape = new Landscape();
landscape.display();
}

}

我已经检查了我的数组语法,我知道它很好,但由于某些原因,Java似乎无法识别我的类属性,并给我带来了所有这些疯狂的错误:

Landscape.java:13: error: illegal start of expression
landscape = {
^
Landscape.java:15: error: not a statement
{'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
^
Landscape.java:15: error: ';' expected
{'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
^
Landscape.java:15: error: illegal start of expression
{'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
^
Landscape.java:16: error: not a statement
{'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:16: error: ';' expected
{'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:16: error: illegal start of expression
{'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:17: error: not a statement
{'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
^
Landscape.java:17: error: ';' expected
{'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
^
Landscape.java:17: error: illegal start of expression
{'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
^
Landscape.java:18: error: not a statement
{'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:18: error: ';' expected
{'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:18: error: illegal start of expression
{'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:19: error: not a statement
{'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
^
Landscape.java:19: error: ';' expected
{'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
^
Landscape.java:19: error: illegal start of expression
{'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
^
Landscape.java:20: error: not a statement
{'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:20: error: ';' expected
{'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:20: error: illegal start of expression
{'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:21: error: not a statement
{'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:21: error: ';' expected
{'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:21: error: illegal start of expression
{'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
^
Landscape.java:22: error: not a statement
{'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'}
^
Landscape.java:22: error: ';' expected
{'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'}
^
24 errors 

我想了解我做错了什么,以及如何修复

构造函数声明不包含返回类型。
public class Landscape {
char[][] landscape;
// Constructor to create the landscape
public Landscape() {
landscape = new char[][]{
{'^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^'},
{'^', ' ', ' ', ' ', ' ', '^', '^', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
{'^', ' ', '^', '^', ' ', ' ', ' ', '^', '^', ' ', '^', '^', '^', '^', '^', '^'},
{'^', ' ', ' ', '^', ' ', ' ', ' ', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
{'^', '^', ' ', '^', ' ', '^', ' ', '^', '^', '^', '^', '^', '^', '^', '^', '^'},
{'^', ' ', ' ', ' ', ' ', ' ', ' ', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
{'^', '^', ' ', ' ', '^', '^', ' ', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
{'^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^'}
};
}
// Function to display the landscape
public void display() {
System.out.println("");
for (char[] element : landscape) {
System.out.print("              ");
for (char character : element) {
System.out.print(character);
}
System.out.println("");
}
System.out.println("");
}
public static void main(String[] args) {
Landscape landscape = new Landscape();
landscape.display();
}
}

相关内容

  • 没有找到相关文章

最新更新