简体   繁体   English

如何检测 C# 中的特定单词

[英]How do I detect a specific word in C#

I am not a coder or anything I am a noob at coding.我不是编码员或任何我在编码方面的菜鸟。 I tried to do Console.ReadLine("stop") then Console.End but it doesn't work.我尝试执行Console.ReadLine("stop")然后执行Console.End但它不起作用。

Console.WriteLine("What's your name?");
Console.ReadLine();

Console.WriteLine("Cool! My Name Is Program Bot!");
Console.ReadLine("stop");
Console.End();

You need to assign to a variable and then check with conditionals您需要分配给一个变量,然后检查条件

        Console.WriteLine("What's your name?");
        Console.ReadLine();

        Console.WriteLine("Cool! My Name Is Program Bot!");
        var output = Console.ReadLine();
        if (output == null || output == "stop") {
            Environment.Exit(0);
        }

You need to check the following items in to understand what's what:您需要检查以下项目以了解什么是什么:

if/else statements -> Is how your code decides how to do something. if/else 语句-> 是您的代码决定如何做某事的方式。

c# variables -> var output is a variable declaration which could also be string output c# 变量-> var output是一个变量声明,也可以是string output

c# methods -> Console.ReadLine() is a method that returns a string value c# 方法-> Console.ReadLine()是一个返回字符串值的方法

if you are interested in having the app continue until you ask it to stop, you can put everything in a "while" loop like this:如果您有兴趣让应用程序继续运行直到您要求它停止,您可以将所有内容都放在一个“while”循环中,如下所示:

bool reachedEnd = false;

while (!reachedEnd) {
    Console.WriteLine ("What's your name?");
    string name = Console.ReadLine ();

    Console.WriteLine ("Cool! My Name Is Program Bot!");
    string endQuestion = Console.ReadLine ();
    if (endQuestion == "stop") {
        reachedEnd = true;
    }
}

in 99% of the cases there is not need to have:在 99% 的情况下,不需要:

Environment.Exit(0);环境.退出(0);

because after the loop is finished there won't be any code left to execute so it will either exit or just hang until you close it yourself or with additional press of a key.因为在循环完成后,将没有任何代码可以执行,所以它会退出或挂起,直到您自己关闭它或再按一个键。 but you want it to close completely, you can add that after the loop and when the loop is finished it will terminate the app.但是你希望它完全关闭,你可以在循环之后添加它,当循环完成时它会终止应用程序。

bool reachedEnd = false;

while (!reachedEnd) {
    Console.WriteLine ("What's your name?");
    string name = Console.ReadLine ();

    Console.WriteLine ("Cool! My Name Is Program Bot!");
    string endQuestion = Console.ReadLine ();
    if (endQuestion == "stop") {
        reachedEnd = true;
    }
}

Environment.Exit(0);

You can use while and until user do not enter stop your program is working.您可以使用while and until user do not enter stop your program is working。

class Program
{
    static void Main(string[] args)
    {
        string text = string.Empty;
        while (text != "stop")
        {
            Console.WriteLine("What's your name?");
            Console.ReadLine();
            Console.WriteLine("Cool! My Name Is Program Bot!");
            text = Console.ReadLine();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM