在id4上,如果语句和输出被一行搞砸了,它就没有完成。 看起来它没有完成,也无法进入else语句。 顺便说一下,我正在尝试制作一个五行的显示功能。 希望我们可以做到这一点。 (使用System在代码中)
namespace amoba_fel3
{
class FiveInARowBoard
{
public FiveInARowBoard( int size )
{
m_size = size;
m_board = new int[m_size, m_size];
}
public void set( int row, int col, int val )
{
m_board[row, col] = val;
}
public void draw()
{
for (int row = 0; row < m_size; row++)
{
for (int col = 0; col < m_size; col++)
{
if (m_board[row, col] == 0)
Console.Write("[.]");
else if (m_board[row, col] == 1)
Console.Write("[O]");
else if (m_board[row, col] == 2)
Console.Write("[X]");
else if (m_board[row, col] == 3)
Console.Write(" " + cords[col] + " ");
else if (m_board[row, col] == 4)
if (col >= 10)
Console.Write(numcords[row]);
else
Console.Write(" " + numcords[row] + " ");
}
Console.WriteLine();
}
}
private int[] numcords = new int[] { 0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20 };
private char[] cords = "AABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private int m_size;
private int[,] m_board;
static void Main(string[] args)
{
FiveInARowBoard board = new FiveInARowBoard(20);
for (int i = 0; i < 20; i++)
{
board.set(0, i, 3);
board.set(i, 0, 4);
}
board.draw();
}
}
}
答案 0 :(得分:0)
据我所知,您要问的是如何正确对齐网格中的行,因为大于9的数字有两位数,并将网格偏移一个字符。您犯的错误是与col
变量而不是row
变量进行比较。
您的代码目前有以下几行:
else if (m_board[row, col] == 4)
if (col >= 10)
Console.Write(numcords[row]);
else
Console.Write(" " + numcords[row] + " ");
将它们更改为:
else if (m_board[row, col] == 4)
if (row >= 10) // check the row not the col
Console.Write(numcords[row] + " "); // still need a trailing space
else
Console.Write(" " + numcords[row] + " "); // leading space when single digit