请考虑以下代码:
public bool IsTwoInductions()
{
List<Induction> inductionList = GetInduction();
int? introTime = 0;
foreach (var items in inductionList)
{
introTime = items.TotalTime;
}
if(introTime == 90)
{
return true;
}
else
{
return false;
}
}
我的列表包含两条TotalTime == 90
和TotalTime == 120
的记录。现在,当我将值输出到控制台时,我得到了
真
真
但它应该是True
和False
..我哪里错了?
答案 0 :(得分:2)
public void IsTwoInductions()
{
List<Induction> inductionList = GetInduction();
int? introTime = 0;
foreach (var items in inductionList)
{
introTime = items.TotalTime;
if(introTime == 90)
{
Console.Write("true ");
}
else
{
Console.Write("false ");
}
}
}
答案 1 :(得分:1)
如果我理解正确:
private void SomeMethod()
{
List<int> inductionList = new List<int>() { 90, 120};
int? introTime = 0;
bool isEquelToNinety = false;
foreach (var items in inductionList)
{
Console.WriteLine(isEquelToNinety=IsOK(items));
}
}
private bool IsOK(int? introTime)
{
if (introTime == 90)
{
return true;
}
else
{
return false;
}
}
答案 2 :(得分:0)
你试过这个吗?
public bool IsTwoInductions()
{
List<Induction> inductionList = GetInduction();
bool isHaving90 = false;
foreach (var items in inductionList)
{
introTime = items.TotalTime;
if(introTime == 90)
{
isHaving90 = true;
break;
}
}
return isHaving90;
}