2016-09-27 106 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.WriteLine(); 
       string answer = Console.ReadLine(); 

       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

如果你需要一個無限循環,你可以使用'while(true){...}'作爲(;;){...}' –

回答

4
if(answer == "Stop") 
{ 
    Environment.Exit(0); 
} 

將退出程序。 (使用return;將具有相同的影響)

或者,你可以使用break;逃離for循環:

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

我建議你使用break;去,這樣就可以執行任何其他位程序關閉前的代碼。

編輯

其他用戶已提到這一點,但你的for循環嚴重構成。您不需要減少i的值以使for循環無限運行。

首先擺脫代碼中的每個i--;

您有一個無限循環兩種選擇:

while(true) - 這是最常用的,它是更具可讀性和常規使用這種方法。

2.for(;;) - 做同樣的事情,本質上,但它是遠不常見的。

+3

或'return'以便從'Main' –

+0

非常感謝! – hallkids1

+0

非常歡迎。請繼續,如果您滿意,請點擊勾號將其標記爲「Answered」。 – ThePerplexedOne