Java 是否帮助中断if/ELSE?
我正在编写一个协议类,其中包括很多if/else。下面是这个类:Java 是否帮助中断if/ELSE?,java,class,switch-statement,if-statement,Java,Class,Switch Statement,If Statement,我正在编写一个协议类,其中包括很多if/else。下面是这个类: public class Protocol { Scanner scan = new Scanner(System.in); private static final int WAITING = 0; private static final int SENTREQUEST = 1; private static final int SENTITEMS = 2; private static
public class Protocol {
Scanner scan = new Scanner(System.in);
private static final int WAITING = 0;
private static final int SENTREQUEST = 1;
private static final int SENTITEMS = 2;
private static final int ANOTHER = 3;
private static final int CHOICE = 4;
private int choice;
private int state = WAITING;
public String processInput(String theInput) {
String theOutput = null;
if (state == WAITING) {
theOutput = "Do you accept the terms of agreement? Y/N?";
state = SENTREQUEST;
} else if (state == SENTREQUEST) {
if (theInput.equalsIgnoreCase("y")) {
theOutput = ": 1. program file 2. pictures 3. documentation";
state = CHOICE;
} else {
theOutput = "Invalid Input!";
state = SENTITEMS;
}
}
else if (state == CHOICE) {
choice = scan.nextInt();
switch(choice) {
case 1: theOutput = "show something";
break;
case 2: theOutput = "show something";
break;
case 3: theOutput = "show something";
break;
}
}
else if (state == SENTITEMS) {
theOutput = "Want another? (y/n)";
state = ANOTHER;
} else if (state == ANOTHER) {
theOutput = "Do you accept the terms of agreement? Y/N?";
if (theInput.equalsIgnoreCase("y")) {
theOutput ="test";
state = SENTREQUEST;
} else {
theOutput = "Bye.";
state = WAITING;
}
}
return theOutput;
}
}
它没有涉及到switch情况,我确信这是一个正确突破if/elses子句的问题,但我找不到问题所在。为了解决类似的问题,我将实现作为枚举。对于每个策略,您都为枚举创建一个新值,并将代码封装到枚举方法中:
public enum Strategy {
FIRST_STRATEGY {
public String process(String input) {
// Implementation for first strategy
return null;
}
},
SECOND_STRATEGY {
public String process(String input) {
// Implementation for second strategy
return null;
}
};
public abstract String process(String input);
}
您可以根据拥有的枚举值应用所选策略,实际上删除if/else语句链:
Strategy chosenStrategy = Strategy.FIRST_STRATEGY;
String output = chosenStrategy.process(input);
这是我应用于我的一个问题的解决方案,也许它不是最优的或更面向对象的。您必须为您的问题选择正确的解决方案,但我希望我的经验能有所帮助。使用如下状态模式:
public class Protocol {
Scanner scan = new Scanner(System.in);
private abstract class State { abstract String doit(String theInput); }
private final class WAITING extends State {
String doit(String theInput) {
state = SENTREQUEST;
return "Do you accept the terms of agreement? Y/N?";
}
}
private final class SENTREQUEST extends State {
String doIt(String theInput) {
if (theInput.equalsIgnoreCase("y")) {
state = CHOICE;
return ": 1. program file 2. pictures 3. documentation";
} else {
state = SENTITEMS;
return "Invalid Input!";
}
}
}
//TODO refactoring to State classes for all
// private static final int SENTITEMS = 2;
// private static final int ANOTHER = 3;
// private static final int CHOICE = 4;*/
private int choice;
private State state = WAITING;
public String processInput(String theInput) {
return state.doIt(theInput);
}
}
你还没有展示这是如何被称为。例如,对它的第一次调用总是将状态设置为Send_REQUEST,然后返回。还不清楚为什么不首先打开状态(或者确实使用枚举)。是否设置了断点来调试它?这就是我通常要从这里开始的地方。了解scan.nextInt()只尝试将下一个标记读取为int可能会很有用,因此首先需要确保下一个标记实际上是int。正如chimoo的注释一样,我很好奇为什么您使用scan.nextInt(),而代码的其余部分使用“theInput”。您是否无意中“吃掉”了输入中的号码并挂断了与nextInt的通话?一些示例程序输出可能有助于理解正在发生的事情。@M.Jessup你完全正确,我将输入转换成一个整数,它现在可以工作了。虽然这是编写同一程序的一种更优雅的方式,但我认为它并没有真正解决原始问题。