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

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

Function doesn't write ReadLine() input to file

问题

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

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

以下是我的代码:

对于写入文件的函数:

  1. static void CStart(string fileName, List<string> libraries, bool mainMethod)
  2. {
  3. string final = "";
  4. string includes = "";
  5. foreach (string s in libraries)
  6. {
  7. includes += $"#include <{s}>\n";
  8. }
  9. includes += "\n";
  10. final += includes;
  11. if (mainMethod)
  12. {
  13. final += "int main(int argc, const char* argv[])\n{\n\t\n}";
  14. }
  15. File.WriteAllText(fileName, final);
  16. }

以及主函数:

  1. static void Main(string[] args)
  2. {
  3. while (true)
  4. {
  5. for (int i = 0; i < 100; i++)
  6. {
  7. Console.WriteLine();
  8. }
  9. Console.WriteLine("欢迎使用编程助手!\n请选择一个选项:\n");
  10. Console.WriteLine("1) 写入C文件\n");
  11. Console.Write("选择:");
  12. string ipt0 = Console.ReadLine();
  13. if (ipt0 == "1")
  14. {
  15. Console.Write("请选择文件路径:");
  16. string filePath = Console.ReadLine();
  17. Console.Write("您想要包含多少个库:");
  18. string ipt = Console.ReadLine();
  19. List<string> libraries = new List<string>();
  20. if (ipt.All(Char.IsDigit))
  21. {
  22. int i = int.Parse(ipt);
  23. for (int j = 0; j < i; ++j)
  24. {
  25. Console.Write("写入库:");
  26. libraries.Add(Console.ReadLine());
  27. }
  28. }
  29. Console.Write("您想要生成一个主函数吗? [Yn]: ");
  30. string yn = Console.ReadLine();
  31. if (yn == "n" || yn == "N")
  32. {
  33. CStart(filePath, libraries, false);
  34. }
  35. else
  36. {
  37. CStart(filePath, libraries, true);
  38. }
  39. }
  40. }
  41. }

文件文本如下:

  1. int main(int argc, const char argv[])
  2. {
  3. }

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

  1. #include <指定的一个库>
  2. #include <指定的一个库>
  3. #include <指定的一个库>
  4. ...
  5. int main(int argc, const char* argv[])
  6. {
  7. }

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

英文:

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:

  1. static void CStart(string fileName, List&lt;string&gt; libraries, bool mainMethod)
  2. {
  3. string final = &quot;&quot;;
  4. string includes = &quot;&quot;;
  5. foreach (string s in libraries)
  6. {
  7. includes += $&quot;#include &lt;{s}&gt;\n&quot;;
  8. }
  9. includes += &quot;\n&quot;;
  10. final += includes;
  11. if (mainMethod)
  12. {
  13. final += &quot;int main(int argc, const char* argv[])\n{\n\t\n}&quot;;
  14. }
  15. File.WriteAllText(fileName, final);
  16. }

And the main function:

  1. static void Main(string[] args)
  2. {
  3. while (true)
  4. {
  5. for (int i = 0; i &lt; 100; i++)
  6. {
  7. Console.WriteLine();
  8. }
  9. Console.WriteLine(&quot;Welcome to Programming Helper!\nSelect an option:\n&quot;);
  10. Console.WriteLine(&quot;1) Write a C file\n&quot;);
  11. Console.Write(&quot;Choice: &quot;);
  12. string ipt0 = Console.ReadLine();
  13. if (ipt0 == &quot;1&quot;)
  14. {
  15. Console.Write(&quot;Please select a file path: &quot;);
  16. string filePath = Console.ReadLine();
  17. Console.Write(&quot;How many libraries would you like to include: &quot;);
  18. string ipt = Console.ReadLine();
  19. List&lt;string&gt; libraries = new List&lt;string&gt;();
  20. if (ipt.All(Char.IsDigit))
  21. {
  22. int i = int.Parse(ipt);
  23. for (int j = 0; j &lt; i; ++j)
  24. {
  25. Console.Write(&quot;Write a library: &quot;);
  26. libraries.Append(Console.ReadLine());
  27. }
  28. }
  29. Console.Write(&quot;Would you like to generate a main function? [Yn]: &quot;);
  30. string yn = Console.ReadLine();
  31. if (yn == &quot;n&quot; || yn == &quot;N&quot;)
  32. {
  33. CStart(filePath, libraries, false);
  34. }
  35. else
  36. {
  37. CStart(filePath, libraries, true);
  38. }
  39. }
  40. }
  41. }

The file text is:

  1. int main(int argc, const char argv[])
  2. {
  3. }

I want my file to be:

  1. #include &lt;one of the libraries specified&gt;
  2. #include &lt;one of the libraries specified&gt;
  3. #include &lt;one of the libraries specified&gt;
  4. ...
  5. int main(int argc, const char* argv[])
  6. {
  7. }

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

答案1

得分: 1

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

  1. libraries.Append(Console.ReadLine());

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

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

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

  1. 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:

  1. 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:

确定