无法将哈希符号移到右侧

  • 本文关键字:符号 哈希 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;
    }
}

试试这个:

   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;
        }
     }

更节省内存的方法是使用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());    
    }

请在代码中只更改少量内容:

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;
    }
}

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

最新更新