如何在控制台中显示一个简单的空心星号矩形? [英] How to display a simple hollow asterisk rectangle in console?

查看:252
本文介绍了如何在控制台中显示一个简单的空心星号矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Could someone advise me on a simple way to implement hollow rectangles in C#?

我已经能够制作一个简单的矩形,但是我看过的空心矩形程序要么包含,要么包含数组,要么非常复杂.例如,另一个论坛上的解决方案似乎太具有挑战性,并且关于CodeReview.SE 的答案太难以理解.

I have been able to make a simple rectangle, but hollow rectangle programs I've looked at either contained or arrays or were pretty convoluted. For instance, the solution on another forum that seems too challenging, and this answer on CodeReview.SE is too difficult to understand.

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

This is what I've done, which displays a simple (filled) rectangle. How to output a hollow rectangle using if logic if possible?

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();
            }
        }
    }
}

推荐答案

应用程序的必要部分可以简化为:

The essential part of your application can be reduced to:

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();
    }
}

这(通过将逻辑放入专用方法中将逻辑和输入分开)是您应该要做的.有关更多信息,请参见关注点分离.

This, by the way (separating the logic and the input by putting the logic in a dedicated method) is what you should be doing. See Separation of concerns for more information.

先前的方法绘制一个填充的矩形,那么如何绘制空心的矩形?

The previous method draws a filled rectangle, so how can you draw a hollow one?

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

Start looking at the output. For instance, for (5, 3), the output is:

*****
*****
*****

而您想要拥有的是

*****
*   *
*****

您该怎么做?在某些情况下,可能是用空格代替了星星.哪一个?

How can you do that? Probably by replacing stars by spaces in some cases. Which ones?

好吧,再次看一下输出.第一行保持不变,因此使用空格代替星号的条件仅限于第一行以外的行,即:

Well, look again at the output. The first row is untouched, so the condition where you use spaces instead of stars is limited to rows other than the first one, that is:

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();
    }
}

现在,您必须在条件中包括其他情况:第一列,最后一列和一行.

Now you must include the other cases in your condition: the first column, and the last column and row.

为了组合条件,可以使用&&||运算符.第一个表示两个操作数都为真时该条件为真,第二个表示第一个或第二个操作数为真.

In order to combine conditions, you can use && and || operators. The first one means that the condition is true if both operands are true, and the second one means that either the first or the second operand is true.

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

It might be that your final condition will become too difficult to read. There are two things you can do. The first thing is to use intermediary variables. For instance:

if (a && b && c && d)
{
}

可以重构为:

var e = a && b;
var f = c && d;
if (e && f)
{
}

(如果有意义)将ab重新组合,并将cd重新组合.您可以做的第二件事是将条件放入单独的方法中,如果您为该方法找到了一个好名字,则可能会提高可读性:

if it makes sense to regroup a with b and c with d. A second thing you can do is to put the condition in a separate method, which may improve readability if you find a good name for the method:

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 && ...
}

希望这是您要做的所有练习.根据课程的进展情况,您可能还对以下方面感兴趣:

This is hopefully all you need to do the exercise. Depending of your progression in the course, you may also be interested in those aspects:

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

  1. You may avoid repeating code in an if/else block, so instead of:

if (...)
{
    Console.Write(" ");
}
else
{
    Console.Write("*");
}

您可能最终只能只写Write():

Console.Write(...)

您可以使用什么 C#运算符?

对于一种在完成工作之前先验证其输入的方法来说,这是一个好习惯.如果您已经了解了什么是异常,如何将它们用于验证widthheight?为什么在当前情况下过滤负值和零值可能有意义(换句话说,如果width等于-5,则应用程序会崩溃)吗?

It is a good practice for a method to validate its input before doing its job. If you've already learnt what exceptions are, how can they be used to validate width and height? Why in the current situation it may make sense to not filter negative and zero values (in other words, would the application crash if, for instance, width is equal to -5)?

这篇关于如何在控制台中显示一个简单的空心星号矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆