Java 嵌套if条件
我在核心java应用程序中面临嵌套if条件的巨大问题 代码摘要如下……我们可以有近20个嵌套if条件 你能告诉我如何优化这段代码吗 在java中避免这种嵌套if条件的更好方法是什么?我可以有20个嵌套if条件,从java应用程序的设计角度来看,这可能是一个巨大的问题 请帮助提供java 1.6版的解决方案Java 嵌套if条件,java,nested-if,Java,Nested If,我在核心java应用程序中面临嵌套if条件的巨大问题 代码摘要如下……我们可以有近20个嵌套if条件 你能告诉我如何优化这段代码吗 在java中避免这种嵌套if条件的更好方法是什么?我可以有20个嵌套if条件,从java应用程序的设计角度来看,这可能是一个巨大的问题 请帮助提供java 1.6版的解决方案 String condition = getCondition(); if (condition.equals(add)) { // add operation add();
String condition = getCondition();
if (condition.equals(add)) { // add operation
add();
if (condition.equals(sub)) {// sub operation
sub();
if (condition.equals(div)) { //div operation
div();
if (condition.equals(cos)) { // cos operation
cos();
}
}
}
}
编辑:我可以做更多的数学运算,比如说再做20次,然后切换工作。20次运算量很大。用
切换
语句代替。当你有很多决定的时候使用这个
注意,只有当您想在
switch
语句中使用String
时,JDK 7才会出现这种情况。在旧版本中,enum
可能会有所帮助。根据您的代码,它应该始终满足条件。等于(添加)
以执行下一行。根据净行中的条件,它永远不会满足下一个条件。它将何时进入下一行代码
检查字符串条件的数量
您可以使用开关
String condition = getCondition();
switch(condition ) {
case add:
add();
break;
case sub:
sub();
break;
// etc...
}
旁注:打开可从
Java7
获得的字符串,您应该使用if-else-if条件,而不是像这样:
String condition = getCondition();
if(condition.equals(add))
add();
else if(condition.equals(sub))
sub();
else if(condition.equals(div))
div();
else if(condition.equals(cos))
cos();
public static void main(String[] args) {
String operation = user set it
Operation oper = getOperation(operation);
oper.execute(3, 4);
}
private static Operation getOperation(String operation) {
return Operation.valueOf(operation.toUpperCase());
}
状态模式:
public enum Operation {
ADD {
int execute(int a, int b) {
return a + b;
}
},
SUB {
int execute(int a, int b) {
return a - b;
}
},
MUL {
@Override
int execute(int a, int b) {
return a * b;
}
},
DIV {
@Override
int execute(int a, int b) {
return a / b;
}
};
abstract int execute(int a, int b);
public static void main(String[] args) {
Operation oper = getOperation();
oper.execute(3, 4);
}
private static Operation getOperation() {
return Operation.MUL;
}
}
像这样:
String condition = getCondition();
if(condition.equals(add))
add();
else if(condition.equals(sub))
sub();
else if(condition.equals(div))
div();
else if(condition.equals(cos))
cos();
public static void main(String[] args) {
String operation = user set it
Operation oper = getOperation(operation);
oper.execute(3, 4);
}
private static Operation getOperation(String operation) {
return Operation.valueOf(operation.toUpperCase());
}
请注意,如果操作为null,则Operation.valueOf可能引发NullPointerException;如果操作不是操作枚举之一,则可能引发IllegalArgumentException。如果不需要嵌套if语句,则可以使用命令模式 首先,设置matcher和命令之间的映射。这些命令遵循一个公共调用接口,如Runnable、Callable或我的示例命令。该示例演示了如何动态创建包装器并使用静态或非静态类。如果实际命令事先未知,则此模式是实用的,因为以后可以添加和删除命令
public class CommandExample {
private interface Command {
public void execute();
}
private Map<String, Command> commands = new HashMap<>();
private void setUp() {
commands.put("add", new Command() {
public void execute() {
add();
}
});
commands.put("sub", new Sub());
commands.put("arg", new Argument("the argument"));
}
private void add() {
System.out.println("Add called");
}
private static class Sub implements Command {
@Override
public void execute() {
System.out.println("Sub called");
}
}
private class Argument implements Command {
private final String arg;
public Argument(String arg) {
this.arg = arg;
}
@Override
public void execute() {
System.out.println("Argument called with arg " + arg
+ " and access to outer class " + CommandExample.this);
}
}
private void execute(String... names) {
for (String name : names) {
Command command = commands.get(name);
if (command != null) {
command.execute();
} else {
System.err.println("Command '" + name
+ "' is not known. Only know " + commands.keySet());
}
}
}
public static void main(String[] args) {
CommandExample commandExample = new CommandExample();
commandExample.setUp();
commandExample.execute("add", "sub", "arg", "unknown");
}
}
公共类命令示例{
专用接口命令{
公共无效执行();
}
私有映射命令=new HashMap();
私有无效设置(){
commands.put(“添加”,新命令(){
public void execute(){
添加();
}
});
commands.put(“sub”,new sub());
commands.put(“arg”,新参数(“参数”);
}
私有void add(){
System.out.println(“添加调用”);
}
私有静态类子实现命令{
@凌驾
public void execute(){
System.out.println(“子调用”);
}
}
私有类参数实现命令{
私有最终字符串arg;
公共参数(字符串arg){
this.arg=arg;
}
@凌驾
public void execute(){
System.out.println(“用arg调用的参数”+arg
+“和访问外部类”+命令示例。这);
}
}
私有void execute(字符串…名称){
for(字符串名称:名称){
Command=commands.get(名称);
if(命令!=null){
command.execute();
}否则{
System.err.println(“命令””+名称
+“'未知。只知道”+命令.keySet());
}
}
}
公共静态void main(字符串[]args){
CommandExample CommandExample=新CommandExample();
commandExample.setUp();
commandExample.execute(“add”、“sub”、“arg”、“unknown”);
}
}
这里有一个示例,说明如何使用枚举进行此操作。首先创建枚举
enum MathOperations{
ADD, SUB, DIV, COS;
}
那么你可以这样使用它
MathOperations m = MathOperations.valueOf(getCondition().toUpperCase);
switch(m) {
case ADD: add(); break;
case SUB: sub(); break;
//and so on...
}
当然,只有当getCondition()
返回的元素是MathOperations
时,它才会起作用。否则您将获得IllegalArgumentException
您也可以尝试使用。您可以将
add
,sub
,div
,cos
…放入一个有序的列表/数组中。然后使用for
循环迭代列表。使用break
运算符并调用适当的方法
final String[] OPERATION_LIST = { "add", "sub", "div", "cos" };
String condition = getCondition();
for (String op : OPERATION_LIST) {
if (condition.equals(op))
getClass().getMethod(op).invoke(this);
else
break;
}
上述for
循环等于嵌套的if
语句。缺点是其他数学方法必须是公共的。如果不是,你需要类似的东西
注意:如果您正在制作一个计算器(是吗?),可能更好。不幸的是,我们不知道所涉及的类型,或者OP使用的Java版本。这可能合适,也可能不合适——在你的回答中,这一点是值得澄清的。@JonSkeet,使用的版本是java 1.6,你能给出一个工作示例,让我可以在我的系统中运行,或者你可以在www.ideone.com上发布java下的内容吗section@Deepak我认为代码适用于您给出的示例(您的方法不带参数). 但是,我将尝试在不确切知道您需要什么的情况下制作一个完整的示例。@Roger。但这里我们不是在添加数字。您只是在调用add(),sub()方法。@Deepak我不知道您是如何获得数字的,它们是否已经可用?一堆?您会在gui中请求它们吗?我试图模仿你最初提出的关于“如果”陈述的问题。你真正的问题是如何建立一个计算器,还是解析和计算方程式?