功能不会将ReadLine()输入写入文件。

huangapple go评论60阅读模式
英文:

Function doesn't write ReadLine() input to file

问题

我正在为了好玩而制作一个助手,并决定用C#编写它。
它的工作方式如下:

用户被提示要包含多少个库
这些库被附加到 List<string>
用户被询问是否要包括一个 main 函数
main 函数正常工作,但包含不正常工作

以下是我的代码:

对于写入文件的函数:

static void CStart(string fileName, List<string> libraries, bool mainMethod)
{
    string final = "";
    string includes = "";
    foreach (string s in libraries)
    {
        includes += $"#include <{s}>\n";
    }
    includes += "\n";
    final += includes;
    if (mainMethod)
    {
        final += "int main(int argc, const char* argv[])\n{\n\t\n}";
    }
    File.WriteAllText(fileName, final);
}

以及主函数:

static void Main(string[] args)
{
    while (true)
    {
        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine();
        }
        Console.WriteLine("欢迎使用编程助手!\n请选择一个选项:\n");
        Console.WriteLine("1) 写入C文件\n");
        Console.Write("选择:");
        string ipt0 = Console.ReadLine();
        if (ipt0 == "1")
        {
            Console.Write("请选择文件路径:");
            string filePath = Console.ReadLine();
            Console.Write("您想要包含多少个库:");
            string ipt = Console.ReadLine();
            List<string> libraries = new List<string>();
            if (ipt.All(Char.IsDigit))
            {
                int i = int.Parse(ipt);
                for (int j = 0; j < i; ++j)
                {
                    Console.Write("写入库:");
                    libraries.Add(Console.ReadLine());
                }
            }
            Console.Write("您想要生成一个主函数吗? [Yn]: ");
            string yn = Console.ReadLine();
            if (yn == "n" || yn == "N")
            {
                CStart(filePath, libraries, false);
            }
            else
            {
                CStart(filePath, libraries, true);
            }
        }
    }
}

文件文本如下:


int main(int argc, const char argv[])
{

}

我想要我的文件如下所示:

#include <指定的一个库>
#include <指定的一个库>
#include <指定的一个库>
...

int main(int argc, const char* argv[])
{

}

注意:... 应该被其他要求包含的库替换。

英文:

I am making a helper for fun and decided to code it in C#.
It works as follows:
> The user is prompted how many libraries they want to include
> The libraries get appended in a List&lt;string&gt;
> The user is asked if they would like to include a main function
> The main function works, the includes don't
Here's my code:

For the function that writes into the file:

static void CStart(string fileName, List&lt;string&gt; libraries, bool mainMethod)
{
    string final = &quot;&quot;;
    string includes = &quot;&quot;;
    foreach (string s in libraries)
    {
        includes += $&quot;#include &lt;{s}&gt;\n&quot;;
    }
    includes += &quot;\n&quot;;
    final += includes;
    if (mainMethod)
    {
        final += &quot;int main(int argc, const char* argv[])\n{\n\t\n}&quot;;
    }
    File.WriteAllText(fileName, final);
}

And the main function:

static void Main(string[] args)
{
    while (true)
    {
        for (int i = 0; i &lt; 100; i++)
        {
            Console.WriteLine();
        }
        Console.WriteLine(&quot;Welcome to Programming Helper!\nSelect an option:\n&quot;);
        Console.WriteLine(&quot;1) Write a C file\n&quot;);
        Console.Write(&quot;Choice: &quot;);
        string ipt0 = Console.ReadLine();
        if (ipt0 == &quot;1&quot;)
        {
            Console.Write(&quot;Please select a file path: &quot;);
            string filePath = Console.ReadLine();
            Console.Write(&quot;How many libraries would you like to include: &quot;);
            string ipt = Console.ReadLine();
            List&lt;string&gt; libraries = new List&lt;string&gt;();
            if (ipt.All(Char.IsDigit))
            {
                int i = int.Parse(ipt);
                for (int j = 0; j &lt; i; ++j)
                {
                    Console.Write(&quot;Write a library: &quot;);
                    libraries.Append(Console.ReadLine());
                }
            }
            Console.Write(&quot;Would you like to generate a main function? [Yn]: &quot;);
            string yn = Console.ReadLine();
            if (yn == &quot;n&quot; || yn == &quot;N&quot;)
            {
                CStart(filePath, libraries, false);
            }
            else
            {
                CStart(filePath, libraries, true);
            }
        }
    }
}

The file text is:


int main(int argc, const char argv[])
{

}

I want my file to be:

#include &lt;one of the libraries specified&gt;
#include &lt;one of the libraries specified&gt;
#include &lt;one of the libraries specified&gt;
...

int main(int argc, const char* argv[])
{

}

Note: ... should be replaced with the other libraries demanded to include

答案1

得分: 1

你现在有一个空的 libraries 集合。请注意,Append

libraries.Append(Console.ReadLine());

是一个 Linq 扩展方法,它创建了一个新的 IEnumerable&lt;string&gt;,然后你将其 忽略。你应该使用 Add 代替:

libraries.Add(Console.ReadLine());
英文:

Well, you have an empty libraries collection. Note, that Append in

libraries.Append(Console.ReadLine());

is a Linq extension method which creates a new IEnumerable&lt;string&gt; which you in turn ignore. You should use Add instead:

libraries.Add(Console.ReadLine());

huangapple
  • 本文由 发表于 2023年2月8日 21:38:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定