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


  }
 }
}
有帮助吗?

解决方案 3

You have to use a conditional statement (if-else, switch, etc.)

See my code below for an example. You may edit it as you wish.

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

其他提示

It loads all files not only Jacks, but since you have hardcoded the text variable into the output and that refers to the Jack-file it's the only file you see.

However, if you want to choose between those three according to the name the user entered, so this works as desired:

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

Something like this:

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

You want to use an 'if' statement to decide whether to print one or the other:

if (name == "Jack") {
    Console.WriteLine(text);
}
else if (name == "Ken") {
     ...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top