[英]Comparing a string AND and OR at the same time
我在使用AND和OR比较字符串时遇到问题。 如果注释为空并且“规则”等于“ A”或“ B”或“ C”,我想显示一个错误。 这意味着,如果“规则”等于“ A”,“ B”或“ C”以外的任何其他内容,并且“注释”为空,则可以,但是在其他情况下,它应该显示错误。
此时,&&仅与equals。(“ C”)和isEmpty一起使用。 我无法使其与“ A”和“ B”同时使用...
结果是如果等于A并且注释不为空,则错误仍然出现,对于B相同,并且对于C可以正常使用...
String comment = commentet.getText().toString();
if (rule.equals("A") || rule.equals("B") || rule.equals("C") && TextUtils.isEmpty(commentaire)) {
commentet.setError("Comment is necessary");
return;
}
你知道如何解决这个问题吗?
谢谢你的帮助。
这项工作:
String comment = commentet.getText().toString();
if ((rule.equals("A") || rule.equals("B") || rule.equals("C")) && TextUtils.isEmpty(commentaire)) {
commentet.setError("Comment is necessary");
return;
}
基本上将A,B,C检查放在括号内。
您可以将regex用于或部分:
if(TextUtils.isEmpty(commentaire) && rule.matches("A|B|C")){ ... }
尝试这个,
String comment = commentet.getText().toString();
if ((rule.equals("A") || rule.equals("B") || rule.equals("C")) && TextUtils.isEmpty(commentaire)) {
commentet.setError("Comment is necessary");
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.