[英]C# Switch Case with if statements not returning me the console.WriteLines
I am trying to create a simple switch case with If-Statements code.我正在尝试使用 If-Statements 代码创建一个简单的 switch case。
I don't get any value back.我没有得到任何回报。
If I put int Temperature = 0;如果我把 int Temperature = 0; the Code should output "Es ist kalt".代码应该输出“Esist kalt”。 But my console doesn't display anything.但是我的控制台没有显示任何内容。
using System;
namespace SwitchCase
{
class Program
{
static void Main(string[] args)
{
int Temperatur = 25;
switch (Temperatur)
{
case 1:
if (Temperatur <= 0)
{
Console.WriteLine("Es ist kalt");
}
break;
case 2:
if (Temperatur >= 25)
{
Console.WriteLine("Es ist überdurchschnittlich warm");
}
break;
case 3:
if (Temperatur <= 13)
{
Console.WriteLine("Es ist mild");
}
break;
};
}
}
}
Your code doesn't make much sense.你的代码没有多大意义。 When Temperatur
is exactly 1
or 2
, it cannot be less than or equal 0
nor greater than or equal 25
.当Temperatur
正好是1
或2
,它不能小于或等于0
也不能大于或等于25
。 So there's no way Temperatur
has a value that gets any of the first two branches of the switch
selected and additionally satisfies the if
in that branch.因此, Temperatur
不可能有一个值来选择switch
的前两个分支中的任何一个,并且另外满足该分支中的if
。
Just using if
and else if
does what you presumably want:只需使用if
和else if
完成您可能想要的操作:
...
if (Temperatur <= 0)
{
Console.WriteLine("Es ist kalt");
}
else if (Temperatur <= 13)
{
Console.WriteLine("Es ist mild");
}
else if (Temperatur >= 25)
{
Console.WriteLine("Es ist überdurchschnittlich warm");
}
...
your if block never reaches a situation where temperature
is 0.您的 if 块永远不会达到temperature
为 0 的情况。
you only have cases for temperatures 1, 2 and 3 ( case 1:
), so if temperature is anything else than those, nothing will happen.你只有温度 1、2 和 3 的case 1:
( case 1:
),所以如果温度不是这些,什么都不会发生。
Therefore, you should use if
/ else
statements:因此,您应该使用if
/ else
语句:
if (Temperatur <= 0)
{
Console.WriteLine("Es ist kalt");
}
else if (Temperatur <= 13)
{
Console.WriteLine("Es ist mild");
}
else
{
Console.WriteLine("Es ist überdurchschnittlich warm");
}
using System;
public class Example
{
public static void Main()
{
if (Temperatur <= 0)
{
Console.WriteLine("Es ist kalt");
}
else if (Temperatur <= 13)
{
Console.WriteLine("Es ist mild");
}
else if (Temperatur >= 25)
{
Console.WriteLine("Es ist überdurchschnittlich warm");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.