java - if 语句中的多个条件 - first 始终为真
问题描述
为什么第一个if
陈述总是正确的?
private String setDepartment (){
int code = Integer.parseInt(JOptionPane.showInputDialog("Enter The Department Code:\n" +
"1:Sales\n" +
"2:Development\n" +
"3:Accounting\n" +
"4:None"));
/*Why this if statement is always true? How do i solve it? */
if (code !=1 || code !=2 || code !=3 || code !=4)
{
JOptionPane.showMessageDialog(null, "Invalid Number.Enter a number between 1-4");
setDepartment();
}
if (code==1){
return "Sales";
}
else if (code==2){
return "Development";
}
else if (code==3){
return "Accounting";
}
else
return "";
}
解决方案
替换||
为&&
:
if (code !=1 && code !=2 && code !=3 && code !=4)
推荐阅读
- java - Android Studio 如何在使用导航视图时将数据从片段传递到片段?
- javascript - 带有延迟加载的 PHP 图像库
- r - R:如何用不是非常小的数字进行绘图?
- azure - Azure Web App VNet 集成 - UNC 路径不可访问
- c# - 如何链接 3 个列表框
- php - PHP 阻止 INSERT Null 或 0 到数据库中
- javascript - 当前时间和 3500 毫秒之前的 Javascript UTC 时间戳
- excel - 如何用数字拆分字符串并将结果输出到不同的行/列?
- html - 将图片插入到 Github 页面?
- heroku - Heroku 应用程序部署在 heroku 上,但 dns 重定向显示页面说那里什么都没有