gpt4 book ai didi

c# - 如何在if语句中说“或”

转载 作者:太空宇宙 更新时间:2023-11-03 18:05:41 24 4
gpt4 key购买 nike

private void caseTextBox_TextChanged(object sender, EventArgs e)
{
var CTcase = caseTextBox.Text;
caseTextBox.CharacterCasing = CharacterCasing.Upper;

if (CTcase == "THRUHOLE")
{
displayLabel3.Text = "0";
}
else if (CTcase == "EIAA")
{
displayLabel3.Text = "1";
}
else if (CTcase == "EIAB")
{
displayLabel3.Text = "2";
}
else if (CTcase == "EIAC")
{
displayLabel3.Text = "3";
}
else if (CTcase == "EIAD")
{
displayLabel3.Text = "4";
}
else
{
displayLabel3.Text = "error";
}
}


在每个语句中,有3个值可以等于显示正确的数字。但是我不想编写15个 if / else语句来使其工作。我尝试使用 |||&&&。但我不断收到错误消息


  运算符不能应用于类型为'bool'和'string'的操作数


 private void caseTextBox_TextChanged(object sender, EventArgs e)
{
var CTcase = caseTextBox.Text;
caseTextBox.CharacterCasing = CharacterCasing.Upper;

if (CTcase == "THRUHOLE")
{
displayLabel3.Text = "0";
}
else if (CTcase == "EIAA" || "1206")
{
displayLabel3.Text = "1";
}

最佳答案

例如:

if (CTcase == "THRUHOLE" || CTcase == "something else") 
{
displayLabel3.Text = "0";
}


但是-对于这种情况-使用枚举。它更具可读性!
https://msdn.microsoft.com/pl-pl/library/sbbt4032.aspx

关于c# - 如何在if语句中说“或”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31164799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com