帮助打破 if/else 吗?

发布于 2024-10-18 13:43:21 字数 1881 浏览 7 评论 0原文

我正在编写一个协议类,其中包含很多 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

探春 2024-10-25 13:43:21

为了解决类似的问题,一旦我将策略模式实现为枚举。对于每个策略,您为枚举创建一个新值,将代码封装在枚举方法中:

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);

这是我的解决方案应用于我的一个问题,也许它不是最优的或者更面向对象的。您必须为您的问题选择正确的解决方案,但我希望我的经验可以有所帮助。

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:

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);

}

You can apply the chosen strategy depending on the enum value you have, actually removing the chain of if/else statements:

Strategy chosenStrategy = Strategy.FIRST_STRATEGY;
String output = chosenStrategy.process(input);

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.

つ可否回来 2024-10-25 13:43:21

使用这样的状态模式:

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);
    }
}

Use State pattern like this:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文