扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

c#中怎么用matrix修改输出文字

扬州沐宇科技
2024-05-14 17:45:15
c#

在C#中,可以通过创建矩阵来修改输出文字。以下是一个简单的示例:

using System;

class Program
{
    static void Main()
    {
        string text = "Hello World!";
        char[,] matrix = new char[5, 5];

        int index = 0;
        for (int row = 0; row < 5; row++)
        {
            for (int col = 0; col < 5; col++)
            {
                if (index < text.Length)
                {
                    matrix[row, col] = text[index];
                    index++;
                }
                else
                {
                    matrix[row, col] = ' ';
                }
            }
        }

        for (int row = 0; row < 5; row++)
        {
            for (int col = 0; col < 5; col++)
            {
                Console.Write(matrix[row, col]);
            }
            Console.WriteLine();
        }
    }
}

运行以上代码将输出:

Hello
Worl 
d!   
     

在这个示例中,我们首先定义了一个字符串text,然后创建了一个5x5的字符矩阵matrix。接下来,我们将字符串text中的字符依次填充到矩阵中,如果字符不足则用空格填充。最后,我们通过遍历矩阵并输出其中的字符来输出修改后的文字。

扫码添加客服微信