gpt4 book ai didi

Java板数组问题

转载 作者:行者123 更新时间:2023-11-30 04:07:27 24 4
gpt4 key购买 nike

我正在开发一个使用 ansi 代码创建战舰游戏的程序。 ansi 代码并不是什么大问题,因为我发现这些代码允许我覆盖打印行。我的计划是用它来将船只放置在已经绘制的板上。我的问题在于放置船只的数组(“0”)。似乎无法克服 nullpointerexception 错误。我觉得这是阻止我完成该计划的唯一因素。任何帮助将不胜感激。

我的程序的一部分如下。

坐标类:

public class Coordinate {
public String piece;
int row, col, type;
public boolean select, empty, hit, ship;
public String [][] shipPiece;

public String section()
{
piece = "i";

empty = true;
select = false;
hit = false;
//ship = false;

if (empty)
{
piece = "\033[33m0\033[0m";
}
else if(!empty)
{
//empty = true;
ship = true;
piece = "0";
}
else if(!hit && ship)
piece = "\033[33m0\033[0m";

return piece;
}

public String placeShip()
{
for(int x = 0; x<=10; x++)
{
for(int y = 0; y<=10; y++)
{
shipPiece[x][y] = piece;
}
}
return piece;
}
}

玩家等级:

public class Player {

private String name1, name2;
//private ArrayList<Coordinate> moves;
//private Coordinate move;
private int playerNumber;
Scanner in = new Scanner(System.in);
Board bawd = new Board();
Coordinate c = new Coordinate();
Ship sh = new Ship();

public Player(){}

public void GetNames()
{
for(playerNumber = 1; playerNumber <= 2; playerNumber++)
{
System.out.print("Player " + playerNumber + ": ");
name1 = in.nextLine();
playerNumber++;
System.out.print("Player " + playerNumber + ": ");
name2 = in.nextLine();
System.out.print("\033[2J");
}
}

public void setShips()
{
bawd.singleP1View();
sh.coordLoc();
System.out.println("\033[2J");
bawd.singleP2View();
sh.coordLoc();
System.out.println("\033[2J");
game();
}

public void game()
{
bawd.p1turn();
c.placeShip();
}

}

最佳答案

shipPiece 数组未初始化。

改变

 public String [][] shipPiece;

 public String [][] shipPiece = new String [11][11];

关于Java板数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20397619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com