2015-02-08 32 views
0

有人能告诉我一个简单的方法来在C#中实现空心矩形吗?如何在控制台中显示一个简单的空心星号矩形?

我已经能够做出一个简单的矩形,但我看过的空心矩形程序包含或数组或非常复杂。例如,another forum上的解决方案似乎太具有挑战性,而this answer on 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(); 
      } 
     } 
    } 
} 

回答

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

这,顺便说一句(分离的逻辑,并通过把逻辑的专用方法的输入)是你什么应该在做。有关更多信息,请参阅Separation of concerns

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

开始查看输出。例如,对于(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(); 
    } 
} 

现在,你必须在你的病情的其他情况:第一列,最后一列和一行。

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

这可能是您的最终状况会变得难以阅读。有两件事你可以做。首先是使用中间变量。例如:如果是有意义的与bcd重组a

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

if (a && b && c && 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# operator

  2. 在执行工作之前验证输入是一种很好的做法。如果您已经了解了例外情况,那么它们如何用于验证widthheight?为什么在目前的情况下,而不是过滤器的负值和零值(换句话说,如果例如width等于-5,应用程序会崩溃)?

+1

我意识到部分答案时,我瞥了一眼你的代码。这写得非常好。 – cinnamon 2015-02-09 00:33:41

0

添加一个IF语句,你绘制的字符:

,如果它是第一个或最后一行,或第一或最后一列,写*
别人的空间。

为了更有趣,在矩形中制作一个可变大小的矩形“洞”。

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

     } 
    } 
} 
+1

你能编辑你的问题来解释它为什么可行吗?为什么'width'而不是'int'用于'width'和'height'? – 2016-03-25 22:36:37

相关问题