C# 判断物料A和物料B是否为空 - foreach循环示例
这是一种可能的C#代码实现,用于判断物料A和物料B是否为空:
// 假设有一个List<string>变量materials存储了物料A、物料B和物料C的值
bool materialAIsEmpty = true;
bool materialBIsEmpty = true;
foreach (string material in materials)
{
if (material == '物料A')
{
materialAIsEmpty = false;
}
else if (material == '物料B')
{
materialBIsEmpty = false;
}
}
if (materialAIsEmpty && materialBIsEmpty)
{
Console.WriteLine('物料A和物料B为空');
}
首先,我们定义了两个bool类型的变量materialAIsEmpty和materialBIsEmpty,初始值都为true。这是为了在循环中判断物料A和物料B是否为空。
然后,我们使用foreach循环遍历List
最后,在循环外部判断materialAIsEmpty和materialBIsEmpty变量的值,如果都为true,则输出提示信息“物料A和物料B为空”。
原文地址: https://www.cveoy.top/t/topic/mpWD 著作权归作者所有。请勿转载和采集!