英文:
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<string>
> 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<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);
}
And the main function:
static void Main(string[] args)
{
while (true)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine();
}
Console.WriteLine("Welcome to Programming Helper!\nSelect an option:\n");
Console.WriteLine("1) Write a C file\n");
Console.Write("Choice: ");
string ipt0 = Console.ReadLine();
if (ipt0 == "1")
{
Console.Write("Please select a file path: ");
string filePath = Console.ReadLine();
Console.Write("How many libraries would you like to include: ");
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("Write a library: ");
libraries.Append(Console.ReadLine());
}
}
Console.Write("Would you like to generate a main function? [Yn]: ");
string yn = Console.ReadLine();
if (yn == "n" || yn == "N")
{
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 <one of the libraries specified>
#include <one of the libraries specified>
#include <one of the libraries specified>
...
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<string>
,然后你将其 忽略。你应该使用 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<string>
which you in turn ignore. You should use Add
instead:
libraries.Add(Console.ReadLine());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论