简体   繁体   English

用空格开始新行

[英]Start new line with spaces

I'm stuck at this problem I would really appreciate if someone can help me solve this problem. 我坚持这个问题如果有人能帮我解决这个问题,我真的很感激。

  1. I want to add spaces for sub-folder like this format down below.(it must be done with recursion) 我想在下面为这个格式添加子文件夹的空格。(必须用递归完成)
  2. I want to add spaces for sub-folder like this format down below.(it must be done with recursion) 我想在下面为这个格式添加子文件夹的空格。(必须用递归完成)

     using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace G14_211115 { class Program { static void Main(string[] args) { string path = @"C:\\Program Files\\FinchVPN"; WriteDirectories(path); Console.ReadKey(); } /* Tasks. * 1. I want to add spaces for subfolder like this format down below. ( it must be done with recursion) * Like this * -------------------------------------------------------------- * Folder 1 * Folder 1.1 * Folder 1.2 * Folder 2 * Folder 2.1 * Folder 2.1.1 * Folder 2.2 * Folder 3 * Folder 4 * * 2. Task 2 I want to retype this code without using recurrence and C# inbuilt functions. */ static void WriteDirectories(string path) { string[] dirs = Directory.GetDirectories(path); for(int i = 0; i < dirs.Length; i++) { Console.WriteLine(dirs[i]); WriteDirectories(dirs[i]); } } } } 

As you are calling WriteDirectories recursivly, you can pass a variable to each call indicating the (recursion)level . 当您以递归方式调用WriteDirectories ,可以将变量传递给指示(recursion)level每个调用。 Using this you can just put some spaces before the output. 使用它你可以在输出之前放置一些空格。

Therefore you would need to modify your code like this: 因此,您需要像这样修改代码:

static void WriteDirectories(string path, int level = 0)
{
    string[] dirs = Directory.GetDirectories(path);

    for (int i = 0; i < dirs.Length; i++)
    {
        for (int j = 0; j < level; j++)
        {
            Console.Write(" ");
        }

        Console.WriteLine(dirs[i]);
        WriteDirectories(dirs[i], (level + 1));
    }
}

If you want more than just one space per level you can simply modify the inner loop, j < 2 * level would give you 2 space characters per level, and so on. 如果你想要每个级别只有一个空格你可以简单地修改内部循环, j < 2 * level会给你每个级别2个空格字符,依此类推。

Update (Thanks to David) 更新(感谢大卫)

Instead of the inner loop you can use new string(' ', level) for creating a new string containing the specified character level -times. 您可以使用new string(' ', level)来创建包含指定字符level时间的新字符串new string(' ', level)而不是内部循环。

So the inner for -loop would be replaced by Console.Write(new string(' ', level)); 所以内部for -loop将被Console.Write(new string(' ', level));替换Console.Write(new string(' ', level)); .

just provide a depth-parameter to your recursive call and add spaces according to the depth. 只需为递归调用提供深度参数,并根据深度添加空格。

       static void WriteDirectories(string path, int depth) {
            string[] dirs = Directory.GetDirectories(path);
            for(int i = 0; i < dirs.Length; i++) {
                 string preSpaces = new String(' ',depth);
                 Console.WriteLine(preSpaces + dirs[i]); 
                 WriteDirectories(dirs[i], depth+1);
            }
       }

Your first call should provide 0 or 1 as depth. 您的第一个电话应该提供0或1作为深度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM