3

我发现PowerScript 的CHOOSE CASE语句非常有用,因为它使代码比很多ifs 和else ifs 更清晰。

这是一个如何工作的例子,来自上面的链接:

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

aCASE 5 to 11是一样的CASE 5, 6, 7, 8, 9, 10, 11

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

4

5 回答 5

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";
}
于 2013-05-21T17:40:39.053 回答
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;
}
于 2013-05-21T17:47:51.990 回答
3

不完全一样。如果你想在 Java 中实现这样的片段,你必须使用if-else[-if]语句。

基本上,它应该如下所示:

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

希望对你有效。:)

于 2013-05-21T17:37:15.773 回答
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();
    }
}
于 2013-05-21T17:49:11.693 回答
0

不可以。您必须使用一系列 if-elseif-else 语句。

于 2013-05-21T17:37:23.977 回答