[英]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.