C# 用户如何输入单词“quot;”;停止“;代码关闭了吗?
我只是需要帮助,因为我找不到我想要的答案。 我希望这段代码在用户输入stop这个词后立即停止。 如果他们有更多的细节需要我补充,请告诉我C# 用户如何输入单词“quot;”;停止“;代码关闭了吗?,c#,C#,我只是需要帮助,因为我找不到我想要的答案。 我希望这段代码在用户输入stop这个词后立即停止。 如果他们有更多的细节需要我补充,请告诉我 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LibraryWork { class Program { static
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;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间库工作
{
班级计划
{
静态void Main(字符串[]参数)
{
var bookList=新列表();
string ansSearch=string.Empty;
字符串搜索=string.Empty;
int i=1;
对于(int zero=0;i>0;i++)
{
控制台。写入(“类型”);
Console.ForegroundColor=ConsoleColor.Cyan;
Console.Write(“'New'”);
Console.ForegroundColor=ConsoleColor.White;
Console.Write(“如果您想输入一本新书,请键入”);
Console.ForegroundColor=ConsoleColor.Green;
控制台。写(“'List'”);
Console.ForegroundColor=ConsoleColor.White;
Console.Write(“查看输入的图书列表。键入”);
Console.ForegroundColor=ConsoleColor.Yellow;
Console.Write(“'Search'”);
Console.ForegroundColor=ConsoleColor.White;
控制台。写(“查找特定的书”);
Console.WriteLine();
字符串answer=Console.ReadLine();
如果(答案=“新”)
{
Console.Write(“请将您的书的条目格式化为:”);
Console.ForegroundColor=ConsoleColor.Red;
控制台。写(“‘书名’,”);
Console.ForegroundColor=ConsoleColor.Blue;
Console.Write(“'Author(first,last)”);
Console.ForegroundColor=ConsoleColor.DarkGreen;
Console.Write(“'Category',”);
Console.ForegroundColor=ConsoleColor.DarkYellow;
控制台。写(“'Dewey Decimal Number.”);
Console.ForegroundColor=ConsoleColor.White;
Console.WriteLine();
bookList.Add(“条目”+i+:“+Console.ReadLine());
继续;
}
如果(答案=“列表”)
{
bookList.ForEach(Console.WriteLine);
Console.WriteLine(“按enter键继续”);
Console.ReadLine();
我--;
继续;
}
如果(答案==“搜索”)
{
WriteLine(“您想搜索什么(标题:完整标题;作者:第一,最后):”;
search=Console.ReadLine();
var results=bookList.Where(x=>x.Contains(search)).ToList();
bool isEmpty=!results.Any();
如果(我是空的)
{
我--;
Console.ForegroundColor=ConsoleColor.Red;
WriteLine(“对不起,我们找不到那个。”);
Console.ForegroundColor=ConsoleColor.White;
继续;
}
foreach(结果中的var结果)
{
控制台写入线(结果);
}
Console.WriteLine(“按Enter键继续”);
Console.ReadLine();
结果:清晰();
我--;
继续;
}
我--;
Console.ForegroundColor=ConsoleColor.Red;
Console.WriteLine(“响应不正确,请重试”);
Console.ForegroundColor=ConsoleColor.White;
}
}
}
}
将退出程序。(使用return;
将产生相同的影响)
或者,您可以使用break代码>要退出for循环,请执行以下操作:
if(answer == "Stop")
{
break;
}
我建议使用break代码>,以便您可以在程序关闭前执行任何其他代码位
编辑
另一个用户已经提到了这一点,但是您的for循环构造得很糟糕。您不需要减小i
的值,就可以使for循环无休止地运行
从摆脱每一个i--代码>在您的代码中
无限循环有两个选项:
1.虽然(正确)
这是最常用的方法,但使用这种方法更具可读性和常规性
2.for(;;)
-基本上做了相同的事情,但它不太常见
将退出程序。(使用return;
将产生相同的影响)
或者,您可以使用break代码>要退出for循环,请执行以下操作:
if(answer == "Stop")
{
break;
}
我建议使用break代码>,以便您可以在程序关闭前执行任何其他代码位
编辑
另一个用户已经提到了这一点,但是您的for循环构造得很糟糕。您不需要减小i
的值,就可以使for循环无休止地运行
从摆脱每一个i--代码>在您的代码中
无限循环有两个选项:
1.虽然(正确)
这是最常用的方法,但使用这种方法更具可读性和常规性
2.for(;;)
-基本上做了相同的事情,但它不太常见。或返回
从主
返回非常欢迎。如果您满意,请点击勾号,将此标记为已回答。如果(回答),情况如何==