溫馨提示×

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

c#
小億
115
2024-05-14 17:45:15
欄目: 編程語言

在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中的字符依次填充到矩陣中,如果字符不足則用空格填充。最后,我們通過遍歷矩陣并輸出其中的字符來輸出修改后的文字。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女