2016-09-27 8 views
0

「停止」と入力するとこの情報を保存する最も簡単な方法は何ですか?だからもし私がプログラムを再開すれば、情報はまだ彼らのものになるだろう。 私が何を意味しているかを明確にするために助けが必要な場合は私に尋ねてください。ユーザーが「停止」と入力した場合、入力した作業を保存する方法

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

関連する問題