使用“else”命令时出现问题

时间:2015-01-28 14:54:20

标签: c# .net

这是我当前的代码,我的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));
    }
}
}

5 个答案:

答案 0 :(得分:8)

您的代码可能如下所示:

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")else if (line == "easter egg") { Console.WriteLine("this code does funk all"); Console.WriteLine("well done on finding an easter egg {0}", UserName); } 进行双重检查对我来说看起来很奇怪,在这种情况下,第二个"复活节彩蛋"分支永远不会执行。可能这里有一些错字?

或者,如果是有意的,那部分代码涉及"复活节彩蛋"分支应该看起来像

{{1}}

答案 1 :(得分:8)

在此特定方案中更适用的是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语句后的字符串进行比较。如果找到匹配项,则将执行该语句的代码。如果未找到匹配项,则将执行默认语句的代码。

答案 2 :(得分:2)

您尝试编写代码的方式不是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子句实现的目标。

答案 3 :(得分:1)

您没有正确使用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");}

答案 4 :(得分:1)

它不起作用的原因是因为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;
}
相关问题