C# If…Else语句C中的表达式项无效#
当我试图在C# If…Else语句C中的表达式项无效#,c#,if-statement,visual-studio-2012,C#,If Statement,Visual Studio 2012,当我试图在VSL studio 2012中创建一个控制台应用程序,根据用户输入的温度输出关于穿什么的建议时,我遇到了错误 “无效的表达式项” 在这段代码中的每一条if语句上。我不知道我在这里做错了什么 如果有人能为我指出解决这个问题的正确方向,那将是令人惊讶的! 多谢各位 if (temp <= 40) { Console.WriteLine(" It is very cold. Put on a heavy coat."); } else if (temp > 40 &am
VSL studio 2012
中创建一个控制台
应用程序,根据用户输入的温度输出关于穿什么的建议时,我遇到了错误
“无效的表达式项”
在这段代码中的每一条if语句上。我不知道我在这里做错了什么
如果有人能为我指出解决这个问题的正确方向,那将是令人惊讶的!
多谢各位
if (temp <= 40)
{
Console.WriteLine(" It is very cold. Put on a heavy coat.");
}
else if (temp > 40 && <= 60)
{
Console.WriteLine("It is cold. Put on a coat.");
}
else if (temp > 60 && <= 70)
{
Console.WriteLine("The temperature is cool. Put on a light jacket.");
}
else if (temp > 70 && <= 80)
{
Console.WriteLine("The temperature is pleasent. You can wear anything you like");
}
else if (temp > 80 && <= 90)
{
Console.WriteLine(" The temperautre is warm, you can wear short sleeves.");
}
else (temp > 90)
{
Console.WriteLine("It is hot. You can wear shorts today.");
}
if(温度40&&60&&70&&80&&90)
{
WriteLine(“天气很热,你今天可以穿短裤。”);
}
这是无效的语法:
else if (temp > 40 && <= 60)
else如果(temp>40&&40&&temp您编写了无效表达式:
(temp > 40 && <= 60)
(temp>40&&40&&temp正如Erik所说,您必须“重复”变量以进行另一次比较。
不能将“else”放在布尔表达式后面(else应该只是“else{}”),应该将if放在else后面,如下所示:
反而
else (temp > 90)
使用
因此,您的整个代码应该是:
if (temp <= 40)
{
Console.WriteLine(" It is very cold. Put on a heavy coat.");
}
else if (temp > 40 && temp <= 60)
{
Console.WriteLine("It is cold. Put on a coat.");
}
else if (temp > 60 && temp <= 70)
{
Console.WriteLine("The temperature is cool. Put on a light jacket.");
}
else if (temp > 70 && temp <= 80)
{
Console.WriteLine("The temperature is pleasent. You can wear anything you like");
}
else if (temp > 80 && temp <= 90)
{
Console.WriteLine(" The temperautre is warm, you can wear short sleeves.");
}
else if (temp > 90)
{
Console.WriteLine("It is hot. You can wear shorts today.");
}
if(温度40&&temp 60&&temp 70&&temp 80&&temp 90)
{
WriteLine(“天气很热,你今天可以穿短裤。”);
}
If应该是,例如,temp>40&&temp在else关键字之后允许有条件吗?我的疏忽太大了…谢谢你指出这一点。谢谢你指出我的疏忽。
else (temp > 90)
else if (temp > 90)
if (temp <= 40)
{
Console.WriteLine(" It is very cold. Put on a heavy coat.");
}
else if (temp > 40 && temp <= 60)
{
Console.WriteLine("It is cold. Put on a coat.");
}
else if (temp > 60 && temp <= 70)
{
Console.WriteLine("The temperature is cool. Put on a light jacket.");
}
else if (temp > 70 && temp <= 80)
{
Console.WriteLine("The temperature is pleasent. You can wear anything you like");
}
else if (temp > 80 && temp <= 90)
{
Console.WriteLine(" The temperautre is warm, you can wear short sleeves.");
}
else if (temp > 90)
{
Console.WriteLine("It is hot. You can wear shorts today.");
}