java - 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");
}
我们有更好的方法来打印以下消息吗?
解决方案
为什么不使用
if(codeExists) {
System.out.println("CodeExists");
}
if(nameExists) {
System.out.println("NameExists");
}
if (!codeExists && !nameExists) {
System.out.println("Another statements to print");
}
推荐阅读
- ios - 最初按中心旋转时围绕点旋转对象
- c - 用 C 编写程序 - 覆盖中断
- javascript - Mongoose 没有在 DB 上找到元素
- c# - 如何在 Mac 的 .NET 控制台应用程序中选择文件 (OpenDialog)?
- django - 自定义鹡鸰权限
- bash - 将输出拆分为两个变量
- javascript - React Native - 如何从我的 fetch 方法中创建一个全局变量
- mysql - 在docker包含(ubuntu 12)上运行cryptDB时获取“错误1105(HY000):(代理)所有后端都已关闭”
- reactjs - 为什么我可以在我的 React 应用程序中使用 react-data-picker?
- .htaccess - 替换 url 路径 htaccess