无法将井号移到右侧

时间:2021-07-23 12:28:03

标签: c# .net

我无法将井号向右移动以获得以下形状。 我下面的代码没有按预期工作,但我需要得到下面的形状。

请问我该怎么做?

                #
               ##
              ###
             ####
            #####
           ######
          #######

               
public class MyProgramTest
{
    public static void StaircaseChallenge(int n)
    {
        for (int i = 1; i <= n; i++) {
            Console.WriteLine(MySpace(i) + HashSign(i));
        }
    }

    public static string HashSign(int n)
    {
        string t = "";

        for (int i = 1; i <= n; i++) {
            t += "#";
        }
        return t;
    }

    public static string MySpace(int n)
    {
        string t = "/t";

        for (int i = 1; i < n; i++)
        {
            t += " ";
        }
        return t;
    }

}

3 个答案:

答案 0 :(得分:1)

试试这个:

   public class MyProgramTest
   {
        public static void StaircaseChallenge(int n)
        {
            for (int i = 1; i <= n; i++)
            {
                Console.WriteLine(" ".PadLeft(n - i+1, ' ')+"#".PadLeft(i,'#'));
            }
   }

或者对您的代码进行一些更改:

public class MyProgramTest
   {
    public static void StaircaseChallenge(int n)
        {
            for (int i = 1; i <= n; i++)
            {
                Console.WriteLine(MySpace(n - i + 1) + HashSign(i));
            }
        }

        public static string HashSign(int n)
        {
            string t = "";

            for (int i = 1; i <= n; i++)
            {
                t += "#";
            }
            return t;
        }

        public static string MySpace(int n)
        {
            string t = string.Empty;

            for (int i = 1; i < n; i++)
            {
                t += " ";
            }
            return t;
        }
     }

答案 1 :(得分:1)

请只更改代码中的几项:

public class MyProgramTest
{    
    public static void StaircaseChallenge(int n)
    {
        for (int i = 1; i <= n; i++) {
            Console.WriteLine(MySpace(i, n) + HashSign(i));
        }
    }

    public static string HashSign(int n)
    {
        string t = "";

        for (int i = 1; i <= n; i++) {
            t += "#";
        }
        return t;
    }

    public static string MySpace(int m, int n)
    {
        string t = "";

        for (int i = 1; i <= n - m; i++)
        {
            t += " ";
        }
        return t;
    }
}

您必须在 MySpace() 函数中传递多个变量 n(行数)以留出空间。当您在 MySpace() 函数中传递行数时,它将留下(行数 - 1)空间。所以如果你输入 5 那么第一次它会留下 4 个空格然后像明智的那样输入“#”。

答案 2 :(得分:1)

使用 StringBuilder 类可以提高内存效率。

对于这种情况,这并不重要,但很高兴知道。

    // define the amount of steps 
    int n=8;              
    // amount of leading whitespaces, for later usage
    int padding=0;
    // this one is the "working" memory, initialized by n + padding whitespaces
    StringBuilder currentLine=new StringBuilder(new string(' ',n+padding));
    // it counts down from the last index to the one indicated by padding
    for (int i = currentLine.Length-1; i >=padding; i--)
    {
        // replace the char at the current index with #; (here: always the index of the last whitespace)
        currentLine[i]='#';
        // display a copy of the current state on the console, 
        Console.WriteLine(currentLine.ToString());    
    }
相关问题