CS1513错误}预期

时间:2016-10-14 11:30:07

标签: c#

我在使用C#

编写的可视化工作室中的以下代码中不断收到CS1513错误
{
    class NotChineseZodiac
    {
        static void Main(string[] args)
        {

            String YearBorn;
            int YearBornInt;
            Console.WriteLine("What was the year of your birth?");
            YearBorn = Console.ReadLine();
            YearBornInt = Convert.ToInt32(YearBorn);


            Console.WriteLine("You are" + (DateTime.Now.Year - YearBornInt));

            if ((DateTime.Now.Year - YearBornInt) < 18);
            Console.WriteLine("You Shall Not PASS");

            else
              Console.WriteLine("Please Proceed");

            Console.ReadLine(); // last enter key

有没有人看到我的错误?

1 个答案:

答案 0 :(得分:5)

你用分号终止了这一行:

if ((DateTime.Now.Year - YearBornInt) < 18);

正确的是:

if ((DateTime.Now.Year - YearBornInt) < 18)
        Console.WriteLine("You Shall Not PASS");
else
        Console.WriteLine("Please Proceed");

或为了更好的可读性

if ((DateTime.Now.Year - YearBornInt) < 18)
{
     Console.WriteLine("You Shall Not PASS");
}
else
{
     Console.WriteLine("Please Proceed");
}