简体   繁体   English

Foreach循环&清除C#中的列表

[英]Foreach loop & Clearing a list in C#

I Have made a simple Program that can save logs. 我做了一个可以保存日志的简单程序。

I have two problems that i really would appreciate if someone wanted to help me with. 如果有人想帮助我,我将非常感谢两个问题。

1.How can I Erase logs in the program I've written on line 123 in the case 4 in my switch? 1.如何清除我在交换机第4种情况下在第123行编写的程序的日志? I tried both logs.Clear(); 我尝试了两个logs.Clear(); and Array.Clear(text, 0, text.Length); 和Array.Clear(text,0,text.Length); without any success. 没有任何成功。

  1. Question is about a Foreach loop on line 109. How can i rewrite the else if so it only say "Your search yeiled no results!" 问题是关于109行上的Foreach循环。如果这样我只能重写“您的搜索没有结果!”,我该如何重写else呢? when the user search on a titel that is not saved in the list? 当用户搜索未保存在列表中的十分之一时?

I have commented the code so it hopefully will be readable. 我已经注释了该代码,因此希望它可读。

 public class loggProgram
{

    public void Loggbok()
    {
        bool isRunning = true;

        //All the essential code for the List and the Array that the user can save logs into  
        string[] text = new string[2];
        text[0] = "title";
        text[1] = "post";
        List<string> logs = new List<string>();

        //A Dictionary for the search option 3 in the switch menu
        Dictionary<string, string> searchWord = new Dictionary<string, string>();

        //While loop with all the user interaction
        while (isRunning)

        {
            //The menu that the user can type 1-5 in order to make a option
            Console.WriteLine("\n\tWelcome to the logbook.\n\tPlease type a number between 1-5.\n");
            Console.WriteLine("\t[1]Create a log to the logbook");
            Console.WriteLine("\t[2]Display all the current logs saved");
            Console.WriteLine("\t[3]Search the logbook ");
            Console.WriteLine("\t[4]Erase all logs ");
            Console.WriteLine("\t[5]Quit ");
            Console.Write("\tChoose a number: ");

            //Take userinput & convert into a number with TryParse
            string MenuOption = Console.ReadLine();
            int userNumber;
            if (int.TryParse(MenuOption, out userNumber))
            {
                Console.WriteLine("\tLoading program... Done!");
            }


            //Take userNumber and let number 1-5 start the switch

            switch (userNumber)
            {

                case 1:
                    {   // Save a new log 
                        Console.Clear();

                        //Let the user type a titel to the log 
                        Console.Write("Titel: ");
                        text[0] = Console.ReadLine();

                        //Let the user type a message and then save it to searchWord
                        Console.Write("Message: ");
                        text[1] = Console.ReadLine();
                        Console.Clear();

                        searchWord[text[0]] = text[1];


                        break;
                    }
                case 2:
                    {
                        //Show all the saved logs with a foreach loop                           
                        Console.Clear();
                        Console.WriteLine("This is currently saved in the logbook:\n ");
                        foreach (KeyValuePair<string, string> e in searchWord)
                        {
                            Console.WriteLine("Titel: " + e.Key + "\nText: " + e.Value);
                        }

                        Console.Write("\nPress any key to get back to the main meny.\n");
                        Console.ReadKey();
                        Console.Clear();
                        break;

                    }

                case 3:
                    {
                        //Search for matching titel with a Dictionary and present a result if a match is found
                        Console.Clear();
                        Console.WriteLine("Search titel:");
                        string wordSearch = Console.ReadLine();

                        //foreach loop that search in the dictionary 
                        foreach (KeyValuePair<string, string> word in searchWord)
                        {
                            if (word.Key == wordSearch)
                            {
                                Console.Clear();
                                Console.Write("Your search yeiled a result!\nThe match is:\n" + "Titel " + word.Key + "\nText " + word.Value);
                                Console.Write("\n\nPress any key to get back to the main meny.\n");
                            }

                            /*
                            I want to let the user know if nothing was found with a WriteLine output. 
                            Right now it always prints out even when the user input is a correct titel,
                            it happens when more then 1 logs is saved so i need to change this
                            so it only prints out when a stirng input is not saved in the list.*/
                            else if (word.Key != wordSearch)
                            {
                                Console.WriteLine("Your search yeiled no results!");
                            }


                        }
                        Console.ReadLine();
                        Console.Clear();

                        break;
                    }


                case 4:
                    {
                        //Reset all logs (it does not work so i need help with this one)
                        Console.Clear();
                        logs.Clear();
                        Array.Clear(text, 0, text.Length);
                        Console.WriteLine("Erasing all logs... Done.");
                        Console.Write("\n\nPress any key to get back to the main meny.\n");
                        Console.ReadLine();
                        Console.Clear();

                        break;
                    }
                case 5:
                    {
                        //Closing the app
                        Console.Clear();
                        Console.WriteLine("Thanks for using the app, goodbye! ");
                        isRunning = false;
                        Console.ReadLine();

                        break;
                    }

                default:
                    {

                        // Tell the user to type 1 - 5
                        Console.Clear();
                        Console.WriteLine("Something went wrong. Choose a number between 1-5.");
                        Console.ReadLine();
                        Console.Clear();

                        break;

                    }
            }

        }

    }



    /* 
      Main method
      This Run Class creates a new instance so that the loggProgram is being called upon.
      Then it starts the program in a new instance. 
    */

    class Run
    {
        static void Main(string[] args)
        {
            new loggProgram().Loggbok();
        }
    }

1/ Line 123, you can clear you list simply initlializing it : 1 /第123行,您可以清除列表,只需对其进行初始化即可:

logs = new List<string>();

2/ To search a result you can use : 2 /要搜索结果,您可以使用:

var found = searchWord.FirstOrDefault(p=>p.Key == myValue);
if(found != null)
     Console.Write("found");
else
     Console.Write("Not found");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM