我的目标是绘制彼此相邻的两个矩形。我编写了绘制矩形的代码,但是我无法绘制2个相邻的矩形。我看到问题出在哪里,但我不知道如何解决问题。非常感谢帮助。使用ASCII字符绘制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();
}
}
预期输出:
+ - - - - - - - + | | | | | | | |+ - - - + | || | | || | | || | + - - - - - - - ++ - - - +
电流输出:
+ - - - - - - - + | | | | | | | | | | | | | | + - - - - - - - ++ - - - + | | | | + - - - +
你一定要明白你在做字符串连接,当你做'DrawRectangle的(8,8)+的DrawRectangle (4,3)'...... –
是的..我知道问题在哪里,它有多愚蠢,但我用完了想法。任何不同的观点来实现同样也是赞赏。 –
此外,这似乎是功课...如果是这样,你应该这样标记... –