How to do a case with multiple conditions?

后端 未结 5 575
傲寒
傲寒 2020-12-29 21:57

In the 1 month experience I\'ve had with any programming language, I\'ve assumed that switch case conditions would accept anything in the parenthes

5条回答
  •  一整个雨季
    2020-12-29 22:52

    For an alternate to switch statement(multiple if conditions), I think the best solution will be using an enum. For example: Consider the case below:-

        public enum EnumExample {
    
      OPTION1{
    
        public double execute() {
          Log.info(CLASS_NAME, "execute", "The is the first option.");
          return void;
        }
    
      },
      OPTION2{
    
        public double execute() {
          Log.info(CLASS_NAME, "execute", "The is the second option.");
          return void;
        }
    
      },
      OPTION3{
    
        public double execute() {
          Log.info(CLASS_NAME, "execute", "The is the third option.");
          return void;
    
      };
    
      public static final String CLASS_NAME = Indicator.class.getName();
    
      public abstract void execute();
    
    }
    

    The above enum can be used in the following fashion:

    EnumExample.OPTION1.execute();
    

    Hopefully this helps you guys.

提交回复
热议问题