如何在控制台中显示一个简单的空心星号矩形?

cin*_*mon 0 c# algorithm loops

有人可以用一种简单的方法告诉我在C#中实现空心矩形吗?

我已经能够制作一个简单的矩形,但我看到的是空心矩形程序,无论是包含还是数组,或者都是非常复杂的.例如,另一个论坛上的解决方案似乎过于具有挑战性,而CodeReview.SE上的这个答案太难理解了.

这就是我所做的,它显示了一个简单的(填充)矩形.if如果可能,如何使用逻辑输出空心矩形?

class Nested_Loops_Hollow_Rectangles
{
    public void RunExercise()
    {
        // how are now supposed to make this hollow?
        // columns are side by side, rows is number of top to bottom
        // see tut
        Console.WriteLine("Welcome to the HollowRectanglePrinter Program.");
        Console.WriteLine("How many columns wide should the rectangle be?"); //i.e. 4
        int iColMax, iRowMax;
        string userChoiceC = Console.ReadLine();
        Int32.TryParse(userChoiceC, out iColMax);
        Console.WriteLine("How many rows tall should the rectangle be?  "); //i.e. 4
        string userChoiceR = Console.ReadLine();
        Int32.TryParse(userChoiceR, out iRowMax);
        Console.WriteLine("Here you go:");

        if (iRowMax > 0 || iColMax > 0) 
        {
            for (int iRow = 0; iRow < iRowMax; iRow++) 
            {
                for (int iCol = 0; iCol < iColMax; iCol++) 
                {
                    Console.Write("*");
                }

                Console.WriteLine();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ars*_*nko 6

您的应用程序的基本部分可以简化为:

private void DrawFillRectangle(int width, int height)
{
    for (int y = 0; y < height; y++) 
    {
        for (int x = 0; x < width; x++) 
        {
            Console.Write("*");
        }

        Console.WriteLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一下(通过将逻辑放在专用方法中来分离逻辑和输入)就是你应该做的.有关详细信息,请参阅关注点分离.

上一个方法绘制一个填充矩形,那么如何绘制一个空心矩形呢?

开始查看输出.例如,对于(5,3),输出为:

*****
*****
*****
Run Code Online (Sandbox Code Playgroud)

你想要的是:

*****
*   *
*****
Run Code Online (Sandbox Code Playgroud)

你怎么能这样做?可能在某些情况下用空格代替星星.哪个?

那么,再看看输出.第一行是未触及的,因此使用空格而不是星形的条件仅限于第一行以外的行,即:

private void DrawRectangle(int width, int height)
{
    for (int y = 0; y < height; y++) 
    {
        for (int x = 0; x < width; x++) 
        {
            if (y > 0)
            {
                // Print either a star or a space.
            }
            else
            {
                Console.Write("*");
            }
        }

        Console.WriteLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您必须在条件中包含其他案例:第一列,最后一列和行.

为了组合条件,您可以使用&&||运算符.第一个意味着如果两个操作数都为真,则条件为真,第二个意味着第一个或第二个操作数为真.

可能是您的最终状况将变得难以阅读.你可以做两件事.第一件事是使用中间变量.例如:

if (a && b && c && d)
{
}
Run Code Online (Sandbox Code Playgroud)

可以重构为:

var e = a && b;
var f = c && d;
if (e && f)
{
}
Run Code Online (Sandbox Code Playgroud)

如果是有意义的重组abc使用d.您可以做的第二件事是将条件放在一个单独的方法中,如果您找到该方法的一个好名称,这可能会提高可读性:

private void DrawRectangle(int width, int height)
{
    for (int y = 0; y < height; y++) 
    {
        for (int x = 0; x < width; x++) 
        {
            if (this.IsInsideRectangle(x, y))
            {
                // Print either a star or a space.
            }
            else
            {
                Console.Write("*");
            }
        }

        Console.WriteLine();
    }
}

private bool IsInsideRectangle(int x, int y)
{
    return y > 0 && ...
}
Run Code Online (Sandbox Code Playgroud)

希望你能做所有这些练习.根据您在课程中的进展,您可能也会对这些方面感兴趣:

  1. 您可以避免在if/else块中重复代码,因此不是:

    if (...)
    {
        Console.Write(" ");
    }
    else
    {
        Console.Write("*");
    }
    
    Run Code Online (Sandbox Code Playgroud)

    你最后可能只写作Write():

    Console.Write(...)
    
    Run Code Online (Sandbox Code Playgroud)

    您可以使用哪种C#运算符

  2. 在完成工作之前验证其输入的方法是一种很好的做法.如果你已经了解的,那么怎样才能用什么异常验证widthheight?为什么在当前情况下,过滤负值和零值可能是有意义的(换句话说,如果应用程序崩溃,例如,width等于-5)?