How to loop a Console.ReadLine?

后端 未结 5 2003
醉酒成梦
醉酒成梦 2021-01-06 13:31

I cannot figure out how to read user-input in a loop (with Console.ReadLine). I\'m trying to create a note that lets me store what ever the user inputs, and exi

相关标签:
5条回答
  • 2021-01-06 13:52

    One way to do it is this:

    List<string> simpleList = new List<string> { "Alpha", "Bravo", "Charlie", "Delta", "Echo" }; //Dummy data source
            Console.WriteLine("Enter a call sign to find in the list. Press X to exit: "); //Prompt
    
            string callSign;
            string exitKey = "x";
    
            while ((callSign = Console.ReadLine()) != exitKey.ToLower()) { //This is where the "Magic" happens
                if (simpleList.Contains(callSign)) {
                    Console.WriteLine($"\"{callSign}\" exists in our simple list");//Output should the list contain our entry
                    Console.WriteLine(""); //Not really relevant, just needed to added spacing between input and output
                }
                else {
                    Console.WriteLine($"\"{callSign}\" does not exist in our simple list"); //Output should the list not contain our entry
                }
                Console.WriteLine("");
                Console.WriteLine("Enter a call sign to find in the list. Press X to exit: ");//Prompt
            }
    

    The line:

    while ((callSign = Console.ReadLine()) != exitKey.ToLower()) {
          ...
    

    is where the loop happens. If the entry does not equal the exitKey, the steps are repeated.

    0 讨论(0)
  • 2021-01-06 13:56

    Per @n0rd's comment, here's how a do...while loop could work:

    string input;
    var myNotes = new List<Note>();
    do{
        input = Console.ReadLine();
        if (!input.Equals("exit", StringComparison.OrdinalIgnoreCase)){
            var note = new Note();
            note.addText(input);
            myNotes.Add(note);
        }
    } while (!input.Equals("exit", StringComparison.OrdinalIgnoreCase));
    
    0 讨论(0)
  • 2021-01-06 14:07

    To loop Console.ReadLine() you can use this

        `List<string> al = new List<string>(); //list to store string values
        while(true)
        {
            string f = Console.ReadLine();
            if(f == null)         //check if string is null or not
            {
                break;
            }
            else
                al.Add(f);        //add strings to list
        }`
    
    0 讨论(0)
  • 2021-01-06 14:09

    You need List of Notes in order to add as many notes as you want. Additionally, you need to first save ReadLine input check if the user really asked to exit otherwise keep adding notes.

    var myNotes = new List<Note>();
    var firstNote = new Note();
    firstNote.addText("Hi there");
    
    Note note;
    while (true)
    {
        var input = Console.ReadLine();
        if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
        {
            break;
        }
        note = new Note();
        note.addText(input);
        myNotes.Add(note);
    }
    
    0 讨论(0)
  • 2021-01-06 14:09

    The general format is to use something like this (a while loop with a break condition):

    // put code above while loop that only needs to be executed once
    while (true) {        
        // get the user input for every iteration, allowing to exit at will
        String line = Console.ReadLine();
        if (line.Equals("exit")) {
            // exit the method.
            return; // use "break" if you just want to exit the loop
        }
        // this is what will happen in the loop body since we didn't exit
        // put whatever note stuff you want to execute again and again in here
    }
    

    You'll want to edit what goes into the body of this loop depending on what exactly you want done with your note instances. But generally, you repeatedly prompt a user for input until some condition is met and then you break out of the loop. You may decided that condition (e.g. "enter 10 notes"; "type exit"; etc.)

    0 讨论(0)
提交回复
热议问题