繁体   English   中英

如何显示带有内容的星号矩形?

[英]How to display asterisk rectangle with content?

使用在C# Corner上找到的代码,我试图创建一个带有文本内容的星号矩形,但它似乎不起作用。 它打印文本“Program to Print Rectangle *'s”,但不打印矩形。 关于如何实现这一目标的任何想法? 代码如下所示:

using System;

public class Program
{
    public static void Main()
    {
        int height = 5;
        int width = 5;

        for (int i = 1; i <= height; i++)
        {
            for (int j = 1; j <= width; j++)
            {
                if ((i == 1 || i == height) || (j == 1 || j == width))
                {
                    Console.Write("*"); //prints at border place
                }
                else
                {
                    Console.Write(" "); //prints inside other than border
                }
            }
            Console.WriteLine();
        }
    }
}

试试这个代码。

string myText = "Hello World";
int width = myText.Length + 2, height = 3;
for (int i = 1; i <= height; i++)
{
    for (int j = 1; j <= width; j++)
    {
        if ((i == 1 || i == height) || (j == 1 || j == width))
            Console.Write("*"); //prints at border place
        else
            Console.Write(myText[j - 2]); //prints inside other than border
    }
    Console.WriteLine();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM