是否有适用于PowerScript的Java等价物" CHOOSE CASE"?

时间:2013-05-21 17:35:17

标签: java

我发现PowerScript's CHOOSE CASE语句非常有用,因为它使代码比很多ifelse if更清晰。

以上链接中的示例如下:

CHOOSE CASE weight
   CASE IS < 16
        Postage=Weight*0.30
        Method="USPS"
   CASE 16 to 48
        Postage=4.50
        Method="UPS"
   CASE ELSE
        Postage=25.00
        Method="FedEx"
END CHOOSE

CASE 5 to 11CASE 5, 6, 7, 8, 9, 10, 11

相同

请注意,CHOOSE CASE不等同于java的switch

5 个答案:

答案 0 :(得分:5)

在Java中,您可以使用多个case语句,但是没有一种很好的方法可以将表达式指定为case限定符,只是文字:

switch(weight) {
  case 1:
  case 2:
  case 3:
    postage = weight * 0.30;
    method = "USPS";
    break;
  case 4:
  case 5:
  case 6:
    postage = 4.5;
    method = "UPS";
    break;
  default:
    postage = 25.0;
    method = "FedEx";
    break;
}

要获得不错的范围,请坚持使用if / else:

if(weight > 0 && weight <= 3) {
  postage = weight * 0.30;
  method = "USPS";
}
else if(weight > 3 && weight <= 6) {
  postage = 4.5;
  method = "UPS";
}
else {
  postage = 25.0;
  method = "FedEx";
}

答案 1 :(得分:4)

如果您的目标是清理决策点,您可以封装决定哪些案例与使用该决策的代码分开应用的代码,如:

enum WeightClass { LOW, MEDIUM, HIGH };

public WeightClass determineWeightClass(int weight)
{
    return (weight < 16) 
        ? WeightClass.LOW 
        : (weight <= 48 
            ? WeightClass.MEDIUM 
            : WeightClass.HIGH);
}

在决定点:

switch(determineWeightClass(weight))
{
    case LOW:
        ...
        break;
    case MEDIUM:
        ...
        break;
    case HIGH:
        ...
        break;
}

答案 2 :(得分:3)

不完全一样。如果要在Java中实现此类片段,则必须使用if-else[-if]语句。

基本上,它应该是这样的:

if (weight < 16) {
   //something
} else if (weight >= 16 && weight <= 48) {
   //something else 
} else {
   //some other thing
}

希望它适合你。 :)

答案 3 :(得分:2)

如果只有3个案例,一系列if / else就可以了。如果你有很多条件,你可以使用一个Navigable地图,并将它与枚举结合起来,以获得漂亮而光滑的设计:

public class Test1 {

    public static void main(String[] args) {
        printDelivery(0);
        printDelivery(5);
        printDelivery(16);
        printDelivery(48);
        printDelivery(50);
    }

    private static void printDelivery(int weight) {
        Delivery d = Delivery.getDelivery(weight);
        System.out.println("Weight: " + weight + " => $" + d.getPostage(weight) + " with " + d.getMethod());
    }

    static enum Delivery {
        LOW_WEIGHT(15) {
             public double getPostage(int weight) { return 0.3 * weight; }
             public String getMethod() { return "USPS"; }
        }, MEDIUM_WEIGHT(47) {
            public double getPostage(int weight) { return 4.5; }
            public String getMethod() { return "UPS"; }
        }, HIGH_WEIGHT(Integer.MAX_VALUE){
            public double getPostage(int weight) { return 25.0; }
            public String getMethod() { return "FedEx"; }
        };
        private static final NavigableMap<Integer, Delivery> deliveries = new TreeMap<> ();
        static {
            for (Delivery e : values()) {
                deliveries.put(e.maxWeight, e);
            }
        }
        private final int maxWeight;
        Delivery(int maxWeight) {
            this.maxWeight = maxWeight;
        }
        public static Delivery getDelivery(int weight) {
            return deliveries.ceilingEntry(weight).getValue();
        }

        abstract double getPostage(int weight);
        abstract String getMethod();
    }
}

答案 4 :(得分:0)

没有。你必须使用一系列if-elseif-else语句。