If ... Else语句C#中的表达式术语无效

时间:2017-02-23 17:41:39

标签: c# if-statement visual-studio-2012

尝试在console中创建一个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.");
}

3 个答案:

答案 0 :(得分:5)

这是无效的语法:

else if (temp > 40 && <= 60)

你需要这样做:

else if (temp > 40 && temp <= 60)

答案 1 :(得分:2)

您写了无效的表达式:

(temp > 40 && <= 60)

格式正确:

(temp > 40 && temp <= 60)

请更正所有无效的表达方式。

答案 2 :(得分:0)

像埃里克说的那样,你必须重复&#34;重复&#34;变量做另一个比较。 你不能把&#34;否则&#34;接着是一个布尔表达式(否则应该只是&#34;否则{}&#34;),你应该把if放在if之后,就像那样:

代替

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.");
            }
相关问题