"else" 命令有问题

trouble with the "else" commands

这是我当前的代码,我的 else 命令不起作用。我该怎么做,如果有人输入了无法识别的内容,控制台会写我很抱歉,这是一个无法识别的命令,请键入命令列表的帮助,但如果命令被识别,它不会写这个

namespace ConsoleApplication1

{
class Program
{

    static void Main(string[] args)
    {



        Console.WriteLine("Enter your name");
        string UserName = Console.ReadLine();
        Console.WriteLine("hello {0} what would you like me to do", UserName);
        do
        {
            string line = Console.ReadLine();
            if (line == "time") Console.WriteLine("its {1}", UserName, System.DateTime.UtcNow);
            if (line == "help") Console.WriteLine("TIME: shows current time and date");
            if (line == "easter egg") Console.WriteLine("this code does funk all");
            if (line == "easter egg") Console.WriteLine("well done on finding an easter egg {0}", UserName);
            if (line == "juggle") Console.WriteLine("im sorry {0} but im not very good at party tricks", UserName);
            if (line == else) Console.WriteLine("im sorry that is an unrecognzied commands type help for a list of commands");

            Console.WriteLine("anything else");

        }
        while (string.Equals(Console.ReadLine(), "yes", StringComparison.InvariantCultureIgnoreCase));
    }
}
}

您的代码可能应该如下所示:

if (line == "time") 
    Console.WriteLine("its {1}", UserName, System.DateTime.UtcNow);
else if (line == "help") 
    Console.WriteLine("TIME: shows current time and date");
else if (line == "easter egg") 
    Console.WriteLine("this code does funk all");
else if (line == "easter egg") 
    Console.WriteLine("well done on finding an easter egg {0}", UserName);
else if (line == "juggle") 
    Console.WriteLine("im sorry {0} but im not very good at party tricks", UserName);
else
    Console.WriteLine("im sorry that is an unrecognzied commands type help for a list of commands");

但是对 if (line == "easter egg") 的双重检查对我来说有不同的结果看起来很奇怪,在这种情况下第二个 "easter egg" 分支将永远不会执行。可能这里有错别字?

或者,如果有意的话,关于 "easter egg" 分支的那部分代码应该看起来像

else if (line == "easter egg") 
{
    Console.WriteLine("this code does funk all");
    Console.WriteLine("well done on finding an easter egg {0}", UserName);
}

您没有正确使用 if/else 语句,下面是如何使用它们。

        if (line == "time") {Console.WriteLine("its {1}", UserName, System.DateTime.UtcNow);}
        else if (line == "help") {Console.WriteLine(string.Format("TIME: {0}", DateTime.Now);}
        else if (line == "easter egg") {Console.WriteLine("this code does fuck all");}
        else if (line == "easter egg") {Console.WriteLine("well done on finding an easter egg {0}", UserName);}
        else if (line == "juggle") {Console.WriteLine("im sorry {0} but im not very good at party tricks", UserName);}
        else { Console.WriteLine("im sorry that is an unrecognzied commands type help for a list of commands");}

更适用于此特定场景的是 switch 语句。当您需要根据一个输入值执行不同的代码时,它很棒。

switch(line)
{
    case "time":       Console.WriteLine("its {1}", UserName, System.DateTime.UtcNow); break; // By the way, why pass in UserName if you aren't going to use it?
    case "help":       Console.WriteLine("TIME: shows current time and date"); break;
    case "easter egg": Console.WriteLine("well done on finding an easter egg {0}", UserName); break;
    case "juggle":     Console.WriteLine("im sorry {0} but im not very good at party tricks", UserName); break;
    default:           Console.WriteLine("im sorry that is an unrecognzied commands type help for a list of commands");
}

在上面的示例中,line 的值将与每个 case 语句之后的字符串进行比较。如果找到匹配项,将执行该语句的代码。如果没有找到匹配项,将执行默认语句的代码。

您尝试编写代码的方式与 if/else 语句的工作方式不同。正确的语法是:

if (a == 0)
{
  // do something when a is 0
}
else
{
  // do something when a isn't 0
}

如果您有多个案例要检查,您可以添加 else if

if (a == 0)
{
  // do something when a is 0
}
else if (a < 0)
{
  // do something when a is less than 0
}
else
{
  // do something when a is greater than 0
}

在您的例子中,您正在检查单个变量是否具有多个值之一。这种模式很常见,它有自己的语法。它被称为 switch 语句。

switch(line)
{
    case "time":
       // do something when value is "time"
       break;
    case "help":
       // do something when value is "help"
       break;
   default:
       // do something when value is any value that you did not explicitly list
       break;
}

注意 switch 语句中的 default 大小写。这就是您试图通过不正确的 line == else 子句实现的目标。

之所以不起作用,是因为else是保留关键字。 elseif 语句本身的一部分,您不能将值与其他值进行比较。请参阅 This link 以获取有关 if - else 的更多信息。

由于其他人已经使用 if-else 语句解决了它,这里是一个带有 switch-case 的示例。它的结构略有不同,但工作原理几乎相同。

string line = Console.ReadLine();
switch(line)
{
    case "time":
        Console.WriteLine("its {1}", UserName, System.DateTime.UtcNow);
        break;
    case "help":
        Console.WriteLine("TIME: shows current time and date");
        break;
    case "easter egg":
        Console.WriteLine("this code does fuck all");
        Console.WriteLine("well done on finding an easter egg {0}", UserName);
        break;
    case "juggle":
        Console.WriteLine("im sorry {0} but im not very good at party tricks", UserName);
        break;
    default:
        Console.WriteLine("im sorry that is an unrecognzied commands type help for a list of commands");
        break;
}