2016-09-27 121 views
0

如果用户输入“Stop”,最简单的方法是保存这些信息。所以,如果我重新开放该计划,信息仍然是他们的。 问我是否需要帮助澄清我的意思。如果用户输入“Stop”,如何保存输入的工作

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace LibraryWork 
{ 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      var bookList = new List<string>(); 
      string ansSearch = String.Empty; 
      string search = String.Empty; 
      int i = 1; 
      for (int zero = 0; i > zero; i++) 
      { 
       Console.Write("Type "); 
       Console.ForegroundColor = ConsoleColor.Cyan; 
       Console.Write("'New'"); 
       Console.ForegroundColor = ConsoleColor.White; 
       Console.Write(" if you would you like to enter a new book. Type "); 
       Console.ForegroundColor = ConsoleColor.Green; 
       Console.Write("'List' "); 
       Console.ForegroundColor = ConsoleColor.White; 
       Console.Write("to see a list of books entered. Type "); 
       Console.ForegroundColor = ConsoleColor.Yellow; 
       Console.Write("'Search' "); 
       Console.ForegroundColor = ConsoleColor.White; 
       Console.Write("to look up a specific book."); 
       Console.Write(" And if you want to exit. Type "); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.Write("'Stop'."); 
       Console.ForegroundColor = ConsoleColor.White; 
       Console.WriteLine(); 




       string answer = Console.ReadLine(); 

       if (answer == "Stop") 
       { 
        return; 
       } 

       if (answer == "New") 
       { 
        Console.Write("Please format the Entry of your book as follows: "); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.Write("'Name of the Book',"); 
        Console.ForegroundColor = ConsoleColor.Blue; 
        Console.Write("'Author (first, last)',"); 
        Console.ForegroundColor = ConsoleColor.DarkGreen; 
        Console.Write("'Category',"); 
        Console.ForegroundColor = ConsoleColor.DarkYellow; 
        Console.Write("'Dewey Decimal Number'."); 
        Console.ForegroundColor = ConsoleColor.White; 
        Console.WriteLine(); 
        bookList.Add("Entry " + i + ": " + Console.ReadLine()); 
        continue; 
       } 
       if (answer == "List") 
       { 
        bookList.ForEach(Console.WriteLine); 
        Console.WriteLine("Press enter to continue"); 
        Console.ReadLine(); 
        i--; 
        continue; 
       } 
       if (answer == "Search") 
       { 
        Console.WriteLine("What would you like to search for (Title: Full Title; Author: first, last): "); 
        search = Console.ReadLine(); 
        var results = bookList.Where(x => x.Contains(search)).ToList(); 
        bool isEmpty = !results.Any(); 
        if (isEmpty) 
        { 
         i--; 
         Console.ForegroundColor = ConsoleColor.Red; 
         Console.WriteLine("Sorry, we could not find that."); 
         Console.ForegroundColor = ConsoleColor.White; 
         continue; 
        } 
        foreach (var result in results) 
        { 
         Console.WriteLine(result); 

        } 




        Console.WriteLine("Press Enter to continue"); 
        Console.ReadLine(); 
        results.Clear(); 
        i--; 
        continue; 
       } 
       i--; 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("Incorrect Response, please try again"); 
       Console.ForegroundColor = ConsoleColor.White; 
      } 

     } 
    } 

} 
+0

您必须将此信息保存在数据库中的文件中。 – MUT

+0

而且我还发现你发布这些问题是因为其他帐户的虚假声誉@thePreplexedOne:D – MUT

+0

我不会发布假问题 – hallkids1

回答

0

由于bookListList<string>,你可以使用:

File.WriteAllLines("books.txt", bookList); 

我建议你逃跑使用break;循环时,键入“停止”,然后实现我后上方提供的代码for循环。

并且应用程序启动时,假设该文件存在,加载备份:基于我说

if(File.Exists("books.txt")) 
{ 
    bookList = File.ReadAllLines("books.txt").ToList(); 
} 

,这里是什么,我说的是全码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 

namespace LibraryWork 
{ 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      var bookList = new List<string>(); 
      if(File.Exists("books.txt")) //Check if file exists. 
      { 
       bookList = File.ReadAllLines("books.txt").ToList(); //Load the file and convert it to a list. 
      } 
      string ansSearch = String.Empty; 
      string search = String.Empty; 
      int i = 1; 
      for (int zero = 0; i > zero; i++) 
      { 
       Console.Write("Type "); 
       Console.ForegroundColor = ConsoleColor.Cyan; 
       Console.Write("'New'"); 
       Console.ForegroundColor = ConsoleColor.White; 
       Console.Write(" if you would you like to enter a new book. Type "); 
       Console.ForegroundColor = ConsoleColor.Green; 
       Console.Write("'List' "); 
       Console.ForegroundColor = ConsoleColor.White; 
       Console.Write("to see a list of books entered. Type "); 
       Console.ForegroundColor = ConsoleColor.Yellow; 
       Console.Write("'Search' "); 
       Console.ForegroundColor = ConsoleColor.White; 
       Console.Write("to look up a specific book."); 
       Console.Write(" And if you want to exit. Type "); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.Write("'Stop'."); 
       Console.ForegroundColor = ConsoleColor.White; 
       Console.WriteLine(); 




       string answer = Console.ReadLine(); 

       if (answer == "Stop") 
       { 
        break; //Escape the loop. 
       } 

       if (answer == "New") 
       { 
        Console.Write("Please format the Entry of your book as follows: "); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.Write("'Name of the Book',"); 
        Console.ForegroundColor = ConsoleColor.Blue; 
        Console.Write("'Author (first, last)',"); 
        Console.ForegroundColor = ConsoleColor.DarkGreen; 
        Console.Write("'Category',"); 
        Console.ForegroundColor = ConsoleColor.DarkYellow; 
        Console.Write("'Dewey Decimal Number'."); 
        Console.ForegroundColor = ConsoleColor.White; 
        Console.WriteLine(); 
        bookList.Add("Entry " + i + ": " + Console.ReadLine()); 
        continue; 
       } 
       if (answer == "List") 
       { 
        bookList.ForEach(Console.WriteLine); 
        Console.WriteLine("Press enter to continue"); 
        Console.ReadLine(); 
        i--; 
        continue; 
       } 
       if (answer == "Search") 
       { 
        Console.WriteLine("What would you like to search for (Title: Full Title; Author: first, last): "); 
        search = Console.ReadLine(); 
        var results = bookList.Where(x => x.Contains(search)).ToList(); 
        bool isEmpty = !results.Any(); 
        if (isEmpty) 
        { 
         i--; 
         Console.ForegroundColor = ConsoleColor.Red; 
         Console.WriteLine("Sorry, we could not find that."); 
         Console.ForegroundColor = ConsoleColor.White; 
         continue; 
        } 
        foreach (var result in results) 
        { 
         Console.WriteLine(result); 

        } 




        Console.WriteLine("Press Enter to continue"); 
        Console.ReadLine(); 
        results.Clear(); 
        i--; 
        continue; 
       } 
       i--; 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("Incorrect Response, please try again"); 
       Console.ForegroundColor = ConsoleColor.White; 
      } 
      //We end up here after breaking from the loop. 
      File.WriteAllLines("books.txt", bookList); //Save our list of books. 
     } 
    } 

} 
+0

非常感谢! – hallkids1