为每个循环在字符串中添加1个额外的字符

时间:2018-09-10 04:30:17

标签: c#

我有一个仅以1个字符“ $”开头的字符串。我创建了一个运行4次的循环,每次,我都希望字符串附加1个额外的“ $”。因此,当程序运行时,应导致以下结果:

$
$$
$$$
$$$$

到目前为止,这是我的尝试:

        string draw = "";
        int counter = 0;

        while (counter < size) // size is 4
        {
            counter++;
            draw += "$\n";
        }

所以目前它导致:

$
$
$
$

一旦我也可以使用它,我还希望每次达到大小后都减少1。 因此,如果大小为4,则应如下所示:

$
$$
$$$
$$$$
$$$
$$
$

5 个答案:

答案 0 :(得分:2)

因为每次在$字符之后插入换行符。 我认为您应该使用另一个变量来存储结果。该代码将起作用:

int size = 4;
string draw = "";
int counter = 0;
string res = "";

while (counter < size) // size is 4
{
    counter++;
    draw += "$";
    res += draw + "\n";
}
while (counter > 0)
{
    counter--;
    draw = draw.Remove(draw.Length - 1, 1);
    res += draw + "\n";
}

使用StringBuilder而不是仅连接字符串以获得更好的性能总是更好的方法:

int size = 4;
int counter = 0;
var sb = new StringBuilder();
while (counter < size) // size is 4
{
    counter++;
    sb.Append("$");
    Console.WriteLine(sb.ToString());
}

要从字符串末尾删除字符,可以使用如下所示的Remove方法:

while (counter > 0) // size is 4
{
    counter--;
    sb.Remove(sb.Length - 1, 1);
    Console.WriteLine(sb.ToString());
}

答案 1 :(得分:2)

您可以使用以下代码

        int size = 4;
        string draw = "";


        while (size>0) // size is 4
        {
            size--;
            draw += "$";
            Console.WriteLine(draw);
        }

        while (draw.Length > 1)
        {
            size++;
            draw = draw.Substring(0, draw.Length - 1);
            Console.WriteLine(draw);
        }

答案 2 :(得分:1)

您引用以下代码,

https://www.csharpstar.com/10-different-number-pattern-programs-in-csharp/

       Console.Write("Enter a number: ");
        int n = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine();

        for(int i = 1; i < n; i++)
        {
            for(int j = 1; j <= i; j++)
                Console.Write("$");
            Console.WriteLine();
        }
        for(int i = n; i >= 0; i--)
        {
            for(int j = 1; j <= i; j++)
                Console.Write("$");
            Console.WriteLine();
        }
        Console.WriteLine();

答案 3 :(得分:0)

您可以使用2个字符串来做到这一点,并且如果您不想使用它,则不需要使用StringBuilder:

        string draw = "";
        string result = "";
        int counter = 0;
        int size = 4;

        while (counter < size)
        {
            counter++;
            draw += "$";
            result += draw + "\n";
        }

        Console.WriteLine("Increase $:");
        Console.WriteLine("");
        Console.WriteLine(result);

        while (counter > 0)
        {
            counter--;
            draw = draw.Remove(0, 1);
            result += draw + "\n";
        }

        Console.WriteLine("Decrease $: ");
        Console.WriteLine("");
        Console.WriteLine(result);

答案 4 :(得分:0)

using System;

namespace starpattern

{
    class Program

    {
        static void Main(string[] args)

        {

            int i, j;

            for (i = 1; i <= 5; i++)

            {

                for (j = 1; j <= i; j++)

                {

                    Console.Write("$");

                }

                Console.WriteLine();

            }

            Console.ReadLine();

        }

    }

}