首页 > 解决方案 > if 条件中的两个布尔值以打印消息

问题描述

如果条件打印值,我正在寻找任何更好的方法来处理这个问题。如果两者都是真的,我正在寻找打印我想打印两条消息,如果不是只有一条消息。

boolean codeExists = false, nameExists = false;
            for (condition) {
                if(condition){
                    codeExists = true;
                }
                if(condition)) {
                    nameExists = true;
                }
            }
            if(codeExists && nameExists) {
                System.out.println("CodeExists");
                System.out.println("NameExists");
            }
            else if(codeExists) {
                System.out.println("CodeExists");
            }
            else if(nameExists) {
                System.out.println("NameExists");
            }
            else {
System.out.println("Another statements to print");
}

我们有更好的方法来打印以下消息吗?

标签: javaif-statement

解决方案


为什么不使用

if(codeExists) {
    System.out.println("CodeExists");
}
if(nameExists) {
    System.out.println("NameExists");
}
if (!codeExists && !nameExists) {
    System.out.println("Another statements to print");
}

推荐阅读