如何组合多个 if (stringname.equals("value"))?

How do I combine multiple if (stringname.equals("value"))?

我有多个字符串必须具有特定值才能触发通知,但我似乎无法找到组合它们的方法

String wppanswer,tbanswer,ctanswer,ssowpanswer,cppanswer;
if  (wppanswer.equals("Yes"))
            (tbanswer.equals("Yes"))
            (ctanswer.equals("Yes"))
            (ssowpanswer.equals("Yes"))
            (ssowpanswer.equals("N/A"))
            (cppanswer.equals("Yes")){

                NotificationCompat.Builder Yes =
                        new NotificationCompat.Builder(this)
                                .setSmallIcon(R.drawable.adccube)
                                .setContentTitle("Success!")
                                .setContentText("YEY! You have everything you need, proceed with work");
                int mNotificationId = 001;
                NotificationManager mNotifyMgr =
                        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                mNotifyMgr.notify(mNotificationId, Yes.build());
            }

提前谢谢大家,如果我需要提供更多信息,我可以做到

编辑我已尝试以下线程无济于事

Thread 1

Thread 2

Thread 3

您必须使用逻辑 AND,即 Java 中的 &&,使 if 块代码仅在所有条件都为真时才执行,例如:

if (wppanswer.equals("Yes") &&
        tbanswer.equals("Yes") &&
        ctanswer.equals("Yes") &&
        ssowpanswer.equals("Yes") &&
        ssowpanswer.equals("N/A") &&
        cppanswer.equals("Yes")){
    //your code here        
}

此外,正如评论中所说,最好在您的代码中使用 "Yes".equals(wppanswer) 样式,以防止 NullPointerException 如果您的 wppanswer 或任何其他对象将被NULL,而你调用它的是 equals() 方法。

为了使代码更具可读性,考虑重构为辅助方法:

// returns true if and only if all objects are equal to each other
public static boolean allEquals(Object first, Object... rest) {
    for (Object o : rest)
        if (!Objects.equals(first, o))  // `Objects#equals` to avoid NPE 
            return false;
    return true;
}

然后在主代码中使用:

if (allEquals("Yes", wppanswer, tbanswer, ctanswer, ssowpanswer, cppanswer) 
    && "N/A".equals(ssowpanswer)) {
    // trigger action
}