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

有人可以用一种简单的方法告诉我在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?"); //ie 4 int iColMax, iRowMax; string userChoiceC = Console.ReadLine(); Int32.TryParse(userChoiceC, out iColMax); Console.WriteLine("How many rows tall should the rectangle be? "); //ie 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(); } } } } 

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

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

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

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

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

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

你想要的是:

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

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

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

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

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

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

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

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

可以重构为:

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

如果将abc重组a 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 && ... } 

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

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

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

    你可能最终只Write()

     Console.Write(...) 

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

  2. 在执行其工作之前validation其输入的方法是一种很好的做法。 如果您已经了解了哪些例外,那么它们如何用于validationwidthheight ? 为什么在当前情况下, 过滤负值和零值可能是有意义的(换句话说,如果width等于-5 ,应用程序是否会崩溃)?

 class emptyRectangle:Shape { byte width; byte height; public emptyRectangle(byte width,byte height) { this.width = width; this.height = height; } public override void area() { Console.WriteLine("\n----------"); for (int i = 0; i < height; i++) { Console.WriteLine(""); for (int k = 0; k < width; k++) { if (i > 0 && k > 0) { if (i < height - 1 && k < width - 1) { Console.Write(" "); } else Console.Write('*'); } else Console.Write("*"); } } } } 

在绘制角色的位置添加一个IF语句:

如果它是第一行或最后一行或第一列或最后一列,请写*
除了空间。

为了更有趣,在矩形中创建一个可变大小的矩形“孔”。