c# Liststring strList = new Liststring; 如果strList 全是1就返回1如果全是2就返回2如果有1也有2就返回3
以下是实现代码:
List<string> strList = new List<string>();
bool hasOne = false;
bool hasTwo = false;
foreach (string str in strList)
{
if (str == "1")
{
hasOne = true;
}
else if (str == "2")
{
hasTwo = true;
}
}
if (hasOne && hasTwo)
{
return 3;
}
else if (hasOne)
{
return 1;
}
else if (hasTwo)
{
return 2;
}
else
{
// 如果列表为空,或者既没有1也没有2,则返回0或者抛出异常,具体取决于需求。
return 0;
}
原文地址: https://www.cveoy.top/t/topic/bVPD 著作权归作者所有。请勿转载和采集!