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

时间:2017-10-23 12:43:12

标签: c# arrays list

我制作了一个可以保存日志的简单程序。

我有两个问题,如果有人想帮助我,我真的很感激。

1.如何删除我在交换机的案例4中第123行写的程序中的日志?我试过logs.Clear();和Array.Clear(text,0,text.Length);没有任何成功。

  1. 问题是关于第109行的Foreach循环。如果是这样,我怎么能重写其他只是说“你的搜索没有结果!”当用户搜索未保存在列表中的标题时?
  2. 我已对代码进行了评论,因此希望它具有可读性。

     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 个答案:

答案 0 :(得分:0)

1 /第123行,您可以清除列表,只需初始化它:

logs = new List<string>();

2 /要搜索结果,您可以使用:

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