为什么它会在行中显示错误:分段[] =新的Coord(行,列);结构者?
public class TreasureMap{
int rows, cols; // How big is the treasure map
Coord [] treasureLocations; // The locations of treasures
Scanner kbd = new Scanner(System.in);
// Prompt the user for info on the treasure map and then create it
// COMPLETE THIS METHOD
public TreasureMap(){
System.out.println("Enter the map size (2 ints): ");
rows = kbd.nextInt();
cols = kbd.nextInt();
treasureLocations[] = new Coord(rows, cols);
public class Coord {
public final int row;
public final int col;
// Constructor,
public Coord(int ir, int ic){
this.row = ir;
this.col = ic;
}
答案 0 :(得分:2)
treasureLocations[] = new Coord(rows, cols);
这一行不是有效的语法。您需要通过在方括号内放置一个int值,将新对象分配给数组内的位置。像这样:
int position = 0;
treasureLocations[position] = new Coord(rows, cols);
您还需要初始化数组。查看程序的顶部,您已经声明了它但未将其分配给任何程序,因此在运行时您将获得空指针异常。像这样:
Coord [] treasureLocations = new Coord[someNumber]; // The locations of treasures
答案 1 :(得分:0)
你正试图制作一个阵列" []"只有一个Coord。