控制台应用程序中,
代码如下,
为什么我输出什么都没有,哪里错了,不是应该输出“待家里么” string weather = "多云"; if (weather != "下雨")
{
if (weather == "晴天")
{
Console.WriteLine("晒太阳");
}
}
else
{
Console.WriteLine("待家里");
}
代码如下,
为什么我输出什么都没有,哪里错了,不是应该输出“待家里么” string weather = "多云"; if (weather != "下雨")
{
if (weather == "晴天")
{
Console.WriteLine("晒太阳");
}
}
else
{
Console.WriteLine("待家里");
}
{
if (weather == "晴天")//不满足这个条件,不执行
{
Console.WriteLine("晒太阳");
}
}
else
{
Console.WriteLine("待家里");
}
string weather = "多云"; if (weather != "下雨")//满足这个条件,进入这个条件
{
if (weather == "晴天")//不满足这个条件,不执行
{
Console.WriteLine("晒太阳");
}
else
{
Console.WriteLine("影响心情");//添加一个条件分支,输出影响心情
}
}
else
{
Console.WriteLine("待家里");
}
如果不添加分支,子句中if 就结束了啊,后面的else也不走了么?
{
if (weather == "晴天")
{
Console.WriteLine("晒太阳");
}
else
{
Console.WriteLine("待家里");
}
}
{
Console.WriteLine("待家里");
}
替换我给你新添加的分支
string weather = "多云"; if (weather != "下雨")//满足这个条件,进入这个条件
{
if (weather == "晴天")//不满足这个条件,不执行
{
Console.WriteLine("晒太阳");
}
else
{
Console.WriteLine("待家里");
}
}
string weather = "多云";
if (weather == "下雨")
{
Console.WriteLine("待家里");
}
else if (weather == "晴天")
{
Console.WriteLine("晒太阳");
}
else
{
Console.WriteLine("你想要的输出内容");
}
{
if (weather == "晴天")
{
Console.WriteLine("晒太阳");
}
}
else
{
Console.WriteLine("待家里");
}
{
if (weather == "晴天")
{
Console.WriteLine("晒太阳");
}else
{呆在家里 这样才会输出} }
else
{
Console.WriteLine("待家里");
}
因为满足了条件if (weather != "下雨"),不会再执行else了
else是跟它上面的 第一个 它可见的 if配对。
第二个if是被{}给括起来了,所以对于它来说不可见,所以else跟第一个if配对。
以你的想法,估计改成6L给出的代码就可以了。