首页 > 解决方案 > 有没有可以用多个符号表示为金字塔的求和函数

问题描述

我想问如何在字符串中用符号“*”表示 int 中的数量,使其在命令行中看起来像金字塔

例如

int a = int.parse(Console.ReadLine())
a = 1 then Console.WriteLine("*")
a = 2 then write"**" 

如此继续 3=*** 4=****

谢谢

标签: c#stringint

解决方案


我发现最好的方法是使用函数“for”两次,使用函数“new string”。这将在控制台中创建一个金字塔形状。

例如

int a = int.parse(Console.ReadLine())
For(int b=0;b<a;b++)
{
Console.Write new string('*',b)
Console.WriteLine("*")
}
For(int c=a;b>0;b--)
{
Console.Write new string('*',c)
Console.WriteLine("*")
}

推荐阅读