用户如何输入“停止”这个词?代码关闭了

本文关键字:代码 停止 何输入 输入 用户 | 更新日期: 2023-09-27 18:09:12

我只是需要帮助,因为我找不到我一直在寻找的答案。我希望这段代码在用户输入单词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.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;
            }
        }
    }
}

用户如何输入“停止”这个词?代码关闭了

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

将退出程序。(使用return;也有同样的效果)

或者,您可以使用break;转义for循环:

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

我建议使用break;,这样你就可以在程序关闭之前执行任何其他的代码位。

编辑

另一个用户已经提到了这一点,但是你的for循环构造得很糟糕。您不需要减少i的值来无休止地运行for循环。

首先去掉代码中的每个i--;

无限循环有两个选项:

1。 while(true) -这是最常用的,使用这种方法更具有可读性和约定俗成

2。 for(;;) -做同样的事情,本质上,但远不常见。