错误4无效的表达式术语'else'

时间:2011-03-24 01:55:16

标签: c# syntax-error

帮助我看到其他问题,但我是c#

的初学者

代码:

namespace Input_Program
{
    class Program
    {
       static void Main()
        {
           char Y = Console.ReadKey().KeyChar;

           Console.WriteLine("Welcome to my bool program!");
           Console.WriteLine("Input a NON capital y or n when told to.");

            if(Y == 'y')
            {
                Console.WriteLine("Thank you,Please wait.....");
                Console.WriteLine("You input Y");

                else;
                {
                    if(Y == 'n')
                    {
                        Console.WriteLine("You input N");
            }
        }
    }
           Console.ReadLine();
}

6 个答案:

答案 0 :(得分:2)

你不应该在别人之后加分号,而你在其他地方之前错过了一个结束语。

namespace Input_Program
{
    class Program
    {
        static void Main()
        {
            char Y = Console.ReadKey().KeyChar;
            Console.WriteLine("Welcome to my bool program!");
            Console.WriteLine("Input a NON capital y or n when told to.");
            if(Y == 'y')
            {
                Console.WriteLine("Thank you,Please wait.....");
                Console.WriteLine("You input Y");
            }
            else
            {
                if(Y == 'n')
                {
                    Console.WriteLine("You input N");
                }
            }
            Console.ReadLine();
        }
    }
}        

答案 1 :(得分:1)

}控制的两个语句之后(if之前),您需要一个右括号else。另外,在else完成后,删除分号。

答案 2 :(得分:1)

else之后没有分号,你需要关闭花括号。重做你的语法:

if(Y == 'y'){
 Console.WriteLine("Thank you,Please wait.....");
 Console.WriteLine("You input Y");
}
else{
 if(Y == 'n'){
  Console.WriteLine("You input N");  
 }      
}

编辑:else{if(Y== 'n'){...}}最好重构为else if(Y=='n'){...}

答案 3 :(得分:1)

任何使用花括号的东西都是“代码块”。定义class时,您可以在花括号中定义一个代码块,如下所示:

public class MyClass
{

}

花括号内的东西被认为是“在那个范围内”。例如:

public class MyClass
{
    // This method is in the scope of MyClass
    public void MyMethod()
    {
        // This variable is in the scope of MyMethod.
        // It is only accessible from within this method because that is where
        // it is defined.
        string myString = "Hello Method.";
    }

    // This variable is in the scope of MyClass.
    // It is accessible within MyClass, including all methods that are also in scope
    // of MyClass
    public string myGlobalString = "Hello Global.";
}

对于IF语句,它们是一系列块语句,只能包含在方法范围内(例如,您不能在类中使用IF语句)。代码只能传递语句提供的给定路径之一。例如:

public class MyClass
{
    public void MyMethod(string myString)
    {
        if (myString == "Hello")
        {
            // Read like English. "If variable myString equals the value 'Hello', then do this code within this block."
        }
        else if (myString == "Goodbye")
        {
            // "...or else if variable myString equals the value 'Goodbye', then do this code within this block instead.
        }
        else if (myString == "Good Morning")
        {
            // "...or else if variable myString equals the value 'Good Morning', then do this code within this block instead.
        }
        else
        {
            // "...or else do this code if variable myString does not match any of the above code statements.
        }

        // You are not required to include "else" or even "else if". You could just do a single IF statement like this:
        if (myString == "hi)
        {
            // "If variable myString equals the value 'hi' then do this, otherwise do nothing."
        }
    }
}

答案 4 :(得分:0)

{
    char Y = Console.ReadKey().KeyChar;
    Console.WriteLine("Welcome to my bool program!")
    Console.WriteLine("Input a NON capital y or n when told to.");

    if(Y == 'y')
    {
        Console.WriteLine("Thank you,Please wait.....");
        Console.WriteLine("You input Y");    
    }else
    {
        if(Y == 'n')
        {
            Console.WriteLine("You input N");
        }
    }

    Console.ReadLine();
}

匹配你的缩进应该可以帮助你看到你需要/不需要关闭大括号的位置,并且你不会在else之后放置一个半冒号。

您也可以将结构更改为if / else,或者将其更改为两个if并获得相同的结果。

{
    char Y = Console.ReadKey().KeyChar;
    Console.WriteLine("Welcome to my bool program!")
    Console.WriteLine("Input a NON capital y or n when told to.");

    if(Y == 'y')
    {
        Console.WriteLine("Thank you,Please wait.....");
        Console.WriteLine("You input Y");    
    }

    if(Y == 'n')
    {
        Console.WriteLine("You input N");
    }

    Console.ReadLine();
}

答案 5 :(得分:0)

我发现了两件事

if(Y == 'y')
        {
            Console.WriteLine("Thank you,Please wait.....");
            Console.WriteLine("You input Y");
            // you need to close the if with a } on this line     
            else; //no semicolon after else
            {
                if(Y == 'n')
                {
                    Console.WriteLine("You input N");
                }
             }
        }