Я не могу правильно заполнить массив 5X5 и успешно распечатать массив.Оба являются методами.4X4 работает отлично.
Я пытался переписать код по-разному, но безуспешно
// assume I have defined everything else
private int[][] board = new int[4][4];
//variables and other stuff
private String title = "Layout";
private String[] images = {"_", "D", "H", "C", "S"};
private final int BLANK = 0;
private final int DIAMONDS = 1;
private final int HEARTS = 2;
private final int CLUBS = 3;
private final int SPADES = 4;
//if it helps i found a error with calling the method. it prints correctly but only partially.
Этот метод работает
public void createBoardLayout1()
{
for (int i = 0; i < board.length; i++)
{
for (int j = 0; j < board[i].length; j++)
{
if (j % 2 == 0)
{
board[i][j] = DIAMONDS;
}
else
{
board[i][j] = BLANK;
}
}
}
System.out.println();
}
Этот метод вызывает у меня проблему
public void createBoardLayout4()
{
for (int i = 0; i < board.length; i++)
{
for (int j = 0; j < board[i].length; j++)
{
if (i == 0)
{
board[i][j] = CLUBS;
}
else if (i == 4)
{
board[i][j] = CLUBS;
}
if (j == 0)
{
board[i][j] = CLUBS;
}
else if (j == 4)
{
board[i][j] = CLUBS;
}
else if (i == 1)
{
board[i][j] = DIAMONDS;
}
else if (i == 3)
{
board[i][j] = DIAMONDS;
}
else if (j == 1)
{
board[i][j] = DIAMONDS;
}
else if (j == 3)
{
board[i][j] = DIAMONDS;
}
else if (i == 2)
{
board[i][j] = SPADES;
}
else if (j == 2)
{
board[i][j] = SPADES;
}
else
{
board[i][j] = BLANK;
}
}
}
System.out.println();
}
Метод, который печатает массив, но не то, что я ожидаю для createBoardLayout4()
public void printBoard()
{
for (int i = 0; i < board.length; i++)
{
System.out.println();
for (int j = 0; j < board[i].length; j++)
{
if (board[i][j] == 0)
{
board[i][j] = BLANK;
System.out.print(images[BLANK] + " ");
}
else if (board[i][j] == 1)
{
board[i][j] = DIAMONDS;
System.out.print(images[DIAMONDS] + " ");
}
else if (board[i][j] == 2)
{
board[i][j] = HEARTS;
System.out.print(images[HEARTS] + " ");
}
else if (board[i][j] == 3)
{
board[i][j] = CLUBS;
System.out.print(images[CLUBS] + " ");
}
else if (board[i][j] == 4)
{
board[i][j] = SPADES;
System.out.print(images[SPADES] + " ");
}
}
}
System.out.println();
System.out.println();
}
createBoardLayout1
вывод:
D _ D _
D _ D _
D _ D _
D _ D _
createBoardLayout4
вывод:
C C C C C
C D _ _ C
C _ S _ C
C _ _ D C
C C C C C
createBoardLayout4
вывод должен быть:
C C C C C
C D D D C
C D S D C
C D D D C
C C C C C