C#如何在数字右侧显示2dim数组

C#如何在数字右侧显示2dim数组,c#,C#,我想显示一个棋盘,但字段显示在数字的左侧而不是右侧 棋盘的描述如下: 使用签名void displayschessboard(ChessPiece[,]chessboard)创建一个方法 此方法显示棋盘,包括行号和列字母(请参见下面的屏幕截图)。 提示:如果行+列为偶数,则使用浅色背景色(例如灰色),否则使用深色背景 颜色(例如暗黄色)。 现在,为每个单元格打印3个空格。 → 从Start方法调用displayschessboard方法 下面的图片是我想要的样子。 在我的头脑中,它应该工作得很好

我想显示一个棋盘,但字段显示在数字的左侧而不是右侧

棋盘的描述如下:

使用签名void displayschessboard(ChessPiece[,]chessboard)创建一个方法

此方法显示棋盘,包括行号和列字母(请参见下面的屏幕截图)。 提示:如果行+列为偶数,则使用浅色背景色(例如灰色),否则使用深色背景 颜色(例如暗黄色)。 现在,为每个单元格打印3个空格。 → 从Start方法调用displayschessboard方法

下面的图片是我想要的样子。 在我的头脑中,它应该工作得很好,但当我运行它时,它就不工作了。

使用系统;
使用System.IO;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间分配1
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
程序myProgram=新程序();
myProgram.Start();
}
私有void Start()
{
棋子[,]棋盘=新棋子[8,8];//创建作为棋盘的2dim数组
棋盘(棋盘);
显示棋盘(棋盘);
}
棋盘(棋子[,]棋盘)//用null值填充棋盘
{
for(int row=0;row
Console.WriteLine-将指定的数据(后跟当前行终止符)写入标准输出流。尝试改为写…

这里有两个问题:

  • 写下数字后,不要换行。使用
    Write()
    而不是
    WriteLine()

  • 在每行之后(以及显示字母的最后一个循环之前),务必使用一条
    WriteLine()
    ,以便在电路板下打印字母

以下是对我有用的代码:

private void DisplayChessboard(ChessPiece[,] chessboard) // Displays the chessboard
{
    int[] numbers = { 8, 7, 6, 5, 4, 3, 2, 1 };
    char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };

    for (int row = 0; row < chessboard.GetLength(0); row++)
    {
        Console.Write(numbers[row]); // displays the numbers
        for (int col = 0; col < chessboard.GetLength(1); col++)// fills chessboard with colors
        {
            if ((row + col) % 2 == 0)
            {
                Console.BackgroundColor = ConsoleColor.Gray;
            }
            else
            {
                Console.BackgroundColor = ConsoleColor.DarkYellow;
            }
            Console.Write("   ");
            Console.ResetColor();
        }
        Console.WriteLine();
    }
    for (int i = 0; i < letters.Length; i++) // displays the letters
    {
        Console.Write($"{letters[i],3}");
    }
    Console.WriteLine();
}
private void display棋盘(棋子[,]棋盘)//显示棋盘
{
int[]数字={8,7,6,5,4,3,2,1};
字符[]字母={a',b',c',d',e',f',g',h'};
for(int row=0;row

另外,我在每个字母显示前删除了一个空格。我通过向输出字符串添加列宽参数来实现这一点。我使用了
{value,width}
格式。

使用
控制台。Write(数字[行])
而不是
WriteLine
应该能帮你很多忙是的,谢谢,我使用Write而不是WriteLine,并在第一行末尾添加了一个控制台WriteLine,谢谢,现在我明白为什么它不起作用了。我添加了用于验证我答案的代码。
private void DisplayChessboard(ChessPiece[,] chessboard) // Displays the chessboard
{
    int[] numbers = { 8, 7, 6, 5, 4, 3, 2, 1 };
    char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };

    for (int row = 0; row < chessboard.GetLength(0); row++)
    {
        Console.Write(numbers[row]); // displays the numbers
        for (int col = 0; col < chessboard.GetLength(1); col++)// fills chessboard with colors
        {
            if ((row + col) % 2 == 0)
            {
                Console.BackgroundColor = ConsoleColor.Gray;
            }
            else
            {
                Console.BackgroundColor = ConsoleColor.DarkYellow;
            }
            Console.Write("   ");
            Console.ResetColor();
        }
        Console.WriteLine();
    }
    for (int i = 0; i < letters.Length; i++) // displays the letters
    {
        Console.Write($"{letters[i],3}");
    }
    Console.WriteLine();
}