0

I have 3 different notepad files, Jack.txt Ken.txt and Wizard.txt how does this work in coding when I want to input in program for example, I input Ken and the program should load Ken.txt file, Jack for Jack.txt and so on.

The coding below is very basic and unfinished at the moment but no matter what I enter it loads the "Jack.txt" file. Would this work if I separate the coding into loop so when I enter "Wizard" it will loop till they find Wizard.txt file, if not an error message should appear.

    //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);


  }
 }
}
4

4 回答 4

1

它不仅会加载 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);
于 2013-08-18T21:12:13.453 回答
1

像这样的东西:

//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);
于 2013-08-18T21:13:58.857 回答
0

您想使用“if”语句来决定是否打印一个或另一个:

if (name == "Jack") {
    Console.WriteLine(text);
}
else if (name == "Ken") {
     ...
}
于 2013-08-18T21:11:07.413 回答
0

您必须使用条件语句(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;
        }
于 2013-08-18T21:23:57.170 回答