我的目标是绘制两个相邻的矩形。我编写了绘制矩形的代码,但无法绘制 2 个相邻的矩形。我知道问题出在哪里,但我不确定如何解决。非常感谢您的帮助。
class DrawRectangles
{
static void Main(){
Console.WriteLine(DrawRectangle(8,8)+DrawRectangle(4,3));
}
static string DrawRectangle(int width,int length){
StringBuilder sb = new StringBuilder();
string first = "+" + " -".StringMultiplier(width-1)+ " + ";
sb.AppendLine(first);
for(int i=0; i<length-1;i++)
sb.AppendLine("|"+" ".StringMultiplier(2*width-1)+"|");
sb.Append(first);
return sb.ToString();
}
}
internal static class StringExtensions
{
public static string StringMultiplier(this string value,int count){
StringBuilder sb = new StringBuilder(count);
for(int i=0;i<count;i++)
sb.Append(value);
return sb.ToString();
}
}
预期输出:
+ - - - - - - - + | | | | | | | |+ - - - + | || | | || | | || | + - - - - - - - ++ - - - +
电流输出:
+ - - - - - - - + | | | | | | | | | | | | | | + - - - - - - - ++ - - - + | | | | + - - - +