帮助打破 if/else 吗?
我正在编写一个协议类,其中包含很多 if/elses..这是该类:
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 case,并且我确信这是正确突破 if/elses 子句的问题但我找不到问题。
I'm writing a protocol class, which includes a lot of if/elses..here's the class:
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;
}
}
It doesn't get to the switch case, and i'm sure it's an issue of breaking out of the if/elses clauses correctly but i can't find the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了解决类似的问题,一旦我将策略模式实现为枚举。对于每个策略,您为枚举创建一个新值,将代码封装在枚举方法中:
您可以根据您拥有的枚举值应用所选策略,实际上删除 if/else 语句链:
这是我的解决方案应用于我的一个问题,也许它不是最优的或者更面向对象的。您必须为您的问题选择正确的解决方案,但我希望我的经验可以有所帮助。
To solve a similar problem, once I implemented the strategy pattern as an enum. For each strategy, you create a new value for the enum, incapsulating the code in an enum method:
You can apply the chosen strategy depending on the enum value you have, actually removing the chain of if/else statements:
This is a solution I applied for a problem of mine, perhaps it's not the optimal or the more object-oriented one. You have to choose the right solution for your problem, but I hope that my experience can help.
使用这样的状态模式:
Use State pattern like this: