[英]How to display a text file into console after user input? in C#
我有3个不同的记事本文件, Jack.txt
Ken.txt
和Wizard.txt
当我想在程序中输入时如何在编码中工作,例如,我输入Ken
并且程序应该加载Ken.txt
文件, Jack
for Jack.txt
等。
下面的编码是非常基本的,目前尚未完成,但无论我输入什么,它都会加载“Jack.txt”文件。 如果我将编码分成循环,那么这是否有效,所以当我输入“向导”时,它将循环直到找到Wizard.txt文件,如果不是应该出现错误消息。
//Prompt for input
System.Console.WriteLine("Please enter the name");
System.Console.Write("Name> ");
string name = System.Console.ReadLine();
// Fetch the file from input
string text = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\Jack.txt");
string text1 = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\Ken.txt");
string text2 = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\Wizard.txt");
// Display the attributes to the console.
System.Console.WriteLine(" ");
System.Console.WriteLine("{0}", text);
}
}
}
它不仅加载所有文件Jacks,而且因为你已经将text
变量硬编码到输出中并且引用了Jack文件,它是你看到的唯一文件。
但是,如果要根据用户输入的名称在这三者之间进行选择,那么这可以根据需要进行操作:
string name = System.Console.ReadLine();
string textContent = "";
string dir = @"D:\Users\Jack\Documents\Test";
if(name.Equals("Jack", StringComparison.OrdinalIgnoreCase))
{
textContent = File.ReadAllText(Path.Combine(dir, "Jack.txt"));
}
else if(name.Equals("Ken", StringComparison.OrdinalIgnoreCase))
{
textContent = File.ReadAllText(Path.Combine(dir, "Ken.txt"));
}
else if(name.Equals("Jack", StringComparison.OrdinalIgnoreCase))
{
textContent = File.ReadAllText(Path.Combine(dir, "Wizard.txt"));
}
else
{
// output error or ask for another name
}
System.Console.WriteLine(" ");
System.Console.WriteLine("{0}", textContent);
像这样的东西:
//Prompt for input
System.Console.WriteLine("Please enter the name");
System.Console.Write("Name> ");
string name = System.Console.ReadLine();
string text;
if (new[] {"Jack", "Ken", "Wizard"}.Contains(name))
{
// Fetch the file from input
text = System.IO.File.ReadAllText(@"D:\Users\Jack\Documents\Test\" + name + ".txt");
}
// Display the attributes to the console.
System.Console.WriteLine("");
System.Console.WriteLine("{0}", text);
您想使用'if'语句来决定是否打印其中一个:
if (name == "Jack") {
Console.WriteLine(text);
}
else if (name == "Ken") {
...
}
您必须使用条件语句(if-else,switch等)
请参阅下面的代码示例。 您可以根据需要进行编辑。
//Prompt for input
System.Console.WriteLine( "Please enter the name" );
System.Console.Write( "Name> " );
string name = System.Console.ReadLine();
/*
* Notice how I don't load the files here?
* If one of the files is 100 MB, your program will use 100 MB of memory and possibly more.
*/
string text;
//Display the attributes to the console.
System.Console.WriteLine( " " );
// Add a conditional switch statement.
switch ( name ) // This is similar to using if-else statements.
{
case "Jack":
text = System.IO.File.ReadAllText( @"D:\Users\Jack\Documents\Test\Jack.txt" );
Console.WriteLine( text );
break; // This is used to leave the switch statement.
case "Ken":
text = System.IO.File.ReadAllText( @"D:\Users\Jack\Documents\Test\Ken.txt" );
Console.WriteLine( text );
break;
case "Wizard":
text = System.IO.File.ReadAllText( @"D:\Users\Jack\Documents\Test\Wizard.txt" );
Console.WriteLine( text );
break;
default: // This is when the program can't match any values above.
Console.WriteLine( "Error! No-one exists with that name!" );
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.