gpt4 book ai didi

C# 圣诞树

转载 作者:太空狗 更新时间:2023-10-29 22:25:58 25 4
gpt4 key购买 nike

我是 C# 的新手,因为我请求帮助我实现这个:

        *        *       ***        *       ***      *****        *       ***      *****     *******         *       ***      *****     *******     ********* 

I just had this code:

class Program
{
static void Main(string[] args)
{
AnotherTriangle ob = new AnotherTriangle();
ob.CreateTriangle();

Console.ReadLine();
}
}
class AnotherTriangle
{
int n;
string result = "*";
public void CreateTriangle()
{
flag1:
Console.Write("Please enter number of triangles of your tree: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine();
if (n <= 0)
{
Console.WriteLine("Wrong data type\n"); goto flag1;
}
string s = "*".PadLeft(n);
for (int i = 0; i < n; i++)
{
Console.WriteLine(s);
s = s.Substring(1) + "**";
for (int j = 0; j < n; j++)
{
Console.WriteLine(s);
}
}
}
}

现在我只有“塔”,没有等边三角形。请帮忙。

最佳答案

Console.WriteLine("Please enter the number of triangles in your tree: ");
int n = int.Parse(Console.ReadLine());

for (int i = 1; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
string branch = new String('*', j);
Console.WriteLine(branch.PadLeft(n + 3) + "*" + branch);
}
}

A working example.

关于C# 圣诞树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32842466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com