Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通常的if语句中是否缺少C#}?_C#_If Statement - Fatal编程技术网

通常的if语句中是否缺少C#}?

通常的if语句中是否缺少C#}?,c#,if-statement,C#,If Statement,我现在正在学习C#,在其中一个练习中,我尝试创建一个IF语句,但VS 2015告诉我缺少一个。我使用MSDN网站上的IF示例重写了脚本,但它仍然说 我必须编写一个控制台应用程序,告诉用户是否是男性。一个布尔变量负责。代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace isMale {

我现在正在学习C#,在其中一个练习中,我尝试创建一个IF语句,但VS 2015告诉我缺少一个。我使用MSDN网站上的IF示例重写了脚本,但它仍然说

我必须编写一个控制台应用程序,告诉用户是否是男性。一个布尔变量负责。代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace isMale
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isMale;
            Console.WriteLine("Are you male? (Press Y to answer \"Yes\" and N to answer \"No\")");
            if (Console.ReadKey().Key != ConsoleKey.Y) ;
            {
                isMale = true;
            } // <--- It tells meh that i have to place an } right here
            else ;
            {
                isMale = false;
            } 
            Console.WriteLine("You are male: " + isMale);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间isMale
{
班级计划
{
静态void Main(字符串[]参数)
{
布尔伊斯梅尔;
Console.WriteLine(“你是男性吗?(按Y键回答“是”,按N键回答“否”);
if(Console.ReadKey().Key!=ConsoleKey.Y);
{
isMale=true;

}//只需删除if和else之后的

if (Console.ReadKey().Key != ConsoleKey.Y)
{
     isMale = true;
}
else
{
     isMale = false;
} 

if
之后和
else
之后都有一个
。此外,在这种情况下,
if
可以编写得更简单:

isMale = (Console.ReadKey().Key != ConsoleKey.Y);

如果
行结尾处没有;。这并不是缺少
{
因为这是一个额外的
。投票以简单的打字方式结束。在
else
之后还有一个
。在if或else之后不能使用分号,这是语法错误。我的天啊,我是一个哑巴,非常感谢你!XD在
else
投票失败的人之后也删除
。有什么特别的建议吗关于stackOverflow的规则:我们不应该回答有拼写错误的问题?如果在某处有说明,请出示该规则。