所以我有这个代码部分:
private int checkErrors() {
int error = 0;
if (!(nether.Text.Equals("true"))) { error += 1; }
if (!(nether.Text.Equals("false"))) { error += 1; }
if (!(fly.Text.Equals("true"))) { error += 1; }
if (!(fly.Text.Equals("false"))) { error += 1; }
if (!(achivments.Text.Equals("true"))) { error += 1; }
if (!(achivments.Text.Equals("false"))) { error += 1; }
if (!(whitelist.Text.Equals("true"))) { error += 1; }
if (!(whitelist.Text.Equals("false"))) { error += 1; }
if (!(pvp.Text.Equals("true"))) { error += 1; }
if (!(pvp.Text.Equals("false"))) { error += 1; }
if (!(commandBlock.Text.Equals("true"))) { error += 1; }
if (!(commandBlock.Text.Equals("false"))) { error += 1; }
if (!(spawnMonster.Text.Equals("true"))) { error += 1; }
if (!(spawnMonster.Text.Equals("false")) { error += 1; }
return error;
}
但无论如何它会让我'error = 7'
,因为当一个陈述为真时另一个陈述是假的
所以我的问题是:
有没有办法将2个字符串与第3个字符串进行比较?
其他示例:我有字符串userInput
,我希望,如果userInput
不等于"a"
或"b"
来执行error += 1;
!
答案 0 :(得分:2)
我怀疑你是否正试图寻找不真实的情况" 和它不是"假"。如下所示:
if (spawnMonster.Text != "true" && spawnMonster.Text != "false")
{
error++;
}
或者,表达条件一次,并将其应用于所有字符串:
var strings = new[] { nether.Text, fly.Text, achivments.Text, whitelist.Text
pvp.Text, commandBlock.Text, spawnMonster.Text };
return strings.Count(t => t != "true" && t != "false");
答案 1 :(得分:0)
只有两个条款:
if (!nether.Text.Equals("true") && !nether.Text.Equals("false")) { error += 1; }
答案 2 :(得分:0)
您可以使用的另一种方法是创建一个接受2个参数的函数。一个参数是userInput,另一个参数是userInput不应该是的值数组。它将返回1或0.
示例:
public int isBadValue(string userInput, string[] goodValues){
if(Array.IndexOf(userInput) > -1){
return 0;
}
return 1;
}
在您的代码中,您可以执行以下操作:
string[] goodValues = {"a", "b"};
error += isBadValue("s", goodValues);
您可以轻松添加更多好的值,该功能将能够处理它。不确定好的值是否会根据输入字段而改变,这就是为什么我没有将它包含在isBadValue函数中。