gpt4 book ai didi

java - switch case 中需要的常量表达式

转载 作者:行者123 更新时间:2023-12-01 10:00:16 26 4
gpt4 key购买 nike

我已经为我的字符串初始化了常量表达式。这里似乎有什么问题?

我收到的错误是“需要常量表达式”。

下面是我的代码,

public class VatCalculator {

private static int ONBOARD_SALE =1;
private static int ONBOARD_SERVICE =2;
private static int PREORDER_SALE =3;
private static int PREORDER_SERVICE =4;

private static final Map<String, Integer> serviceMap = new HashMap<String, Integer>();
static {
serviceMap.put("onboardSale", ONBOARD_SALE);
serviceMap.put("onboardService", ONBOARD_SERVICE);
serviceMap.put("preorderSale", PREORDER_SALE);
serviceMap.put("preorderService", PREORDER_SERVICE);
}



public static double calculate(String serviceGroup) {

int type = serviceMap.get(serviceGroup);

switch(type) {
//error at below line
case ONBOARD_SALE:
//do something

default:break;
}
}

最佳答案

使用这个:

private static final int ONBOARD_SALE = 1;

关于java - switch case 中需要的常量表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36865112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com