使用©;绘制一个三角形;符号
本文关键字:一个 三角形 符号 #169 绘制 使用 | 更新日期: 2023-09-27 18:25:42
看看这个问题:编写一个程序,打印一个由9个版权符号©
组成的三角形
控制台应该使用这些©
符号打印一个简单的三角形。三角形必须是空的,而不是用©
之类的东西填充。
我更改了控制台字符编码:
Console.OutputEncoding = Encoding.UTF8;
因此CCD_ 4正确打印。我还更改了字体。
我一直在试图找到一种方法来绘制三角形,方法是使用作为循环,但我的新手的知识显然仍然不足以完成这类任务。
你能给我一些关于如何用为循环绘制三角形的提示吗?
顺便说一句,我发现问题是这样解决的:
Console.OutputEncoding = Encoding.UTF8;
char copyRight = ''u00A9';
Console.WriteLine("{0,4}'n{0,3}{0,2}'n{0,2}{0,4}'n{0}{0,2}{0,2}{0,2}", copyRight);
你能给我一些关于如何用for循环画三角形的提示吗?
当然。首先:你能写一个你理解的程序吗?它可以在不使用任何循环的情况下绘制三角形?
一旦你做到了,你能用一个更简单的循环来写程序吗,比如while
?
如果没有,请在处理for
之前了解while
的工作原理。您应该能够清楚地回答以下问题:"if(condition) statement
和while(condition) statement
形式的代码之间到底有什么区别?"
一旦你做到了,你能把用while
编写的正确程序转换成用for
编写的等效程序吗?CCD_ 12和CCD_。
这里有一个简单的例子。它看起来比实际情况更可怕。
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Draw(' ', ''u00A9', 20, true);
}
static void Draw(char sym, char back, int length, bool padding)
{
if (length < 5)
{
length = 5;
}
else if ((length % 2) == 0) // Ensure an odd length using a modulo operation
{
length++;
}
if (padding)
{
Console.WriteLine(new string(back, length));
}
int mid = length / 2;
int left = mid;
int right = mid + 1;
do
{
Console.Write(new string(back, left));
Console.Write(new string(sym, right - left));
Console.Write(new string(back, length - right));
Console.WriteLine();
left--;
right++;
}
while (left != 0);
if (padding)
{
Console.WriteLine(new string(back, length));
}
Console.WriteLine();
}
输出:
©©©©©©©©©
©©©© ©©©©
©©© ©©©
©© ©©
© ©
©©©©©©©©©
9个符号的替代版本:
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Render(Draw(''u00A9', 4));
Render(Draw(''u00A9', 8));
Render(Draw(''u00A9', 16));
}
static char[,] Draw(char sym, int height)
{
int width = (height * 2) - 1;
char[,] map = new char[height, width];
int x = width - 1;
int y = height - 1;
for (int i = 0; i < height; i++)
{
map[y, i * 2] = sym;
if (i != 0)
{
map[y - i, i] = sym;
map[y - i, x - i] = sym;
}
}
return map;
}
static void Render(char[,] map)
{
int width = map.GetLength(1);
int height = map.GetLength(0);
for (int i = 0; i < height; i++)
{
if (i != 0)
{
Console.WriteLine();
}
for (int j = 0; j < width; j++)
{
char c = map[i, j];
Console.Write(c == ''0' ? ' ' : c);
}
}
Console.WriteLine();
}
输出:
©
© ©
© ©
© © © ©
©
© ©
© ©
© ©
© ©
© ©
© ©
© © © © © © © ©
©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© © © © © © © © © © © © © © © ©
我一直想知道没有人使用PadLeft和PadRight:
public void DrawEmptyTriangle( char symbol, char padSymbol, int triangleBase)
{
// heuristics - this is because of the 2nd line
int alignment = triangleBase - 3;
int half = triangleBase / 2 + 1;
// top and bottom line are different from the others,
// so it is easier to generate them separately
string top = symbol.ToString ( ).PadLeft ( half, padSymbol).PadRight ( triangleBase, padSymbol);
// if this is new to you - Enumerable.Repeat will generate a sequence of n character elements
// string.Join will make a string inserting, in this case, a space between them
string bottom = string.Join ( " ", Enumerable.Repeat ( 'x', half ) );
Console.WriteLine ( top );
for ( int index = 1; index < triangleBase; index+=2 )
{
string line =
string.Format ( "{0}{1}{0}", symbol, new string ( padSymbol, index ) )
.PadLeft ( alignment, padSymbol )
.PadRight ( alignment, padSymbol );
alignment++;
Console.WriteLine ( line );
}
Console.WriteLine ( bottom );
}
阅读代码中的注释,并使用以下方法:)