在我的CustomerTypeApp课堂上,我需要更改getDiscountPercent方法以使用 switch 而不是 if 语句链。这是 if 语句版本:
public static double getDiscountPercent(CustomerType ct) {
double discountPercent = 0;
if (ct == CustomerType.RETAIL) {
discountPercent = 0.156;
} else if (ct == CustomerType.TRADE) {
discountPercent = 0.30;
} else if (ct == CustomerType.COLLEGE) {
discountPercent = 0.20;
}
return discountPercent;
}
}
以下是我尝试过的 switch 语句,但收到了错误:
枚举 switch case 标签必须是枚举常量的非限定名称
double discountPercent = 0;
switch(ct) {
case CustomerType.RETAIL :
discountPercent = 0.156;
break;
case CustomerType.TRADE :
discountPercent = 0.30;
break;
case CustomerType.COLLEGE :
discountPercent = 0.20;
break;
default :
discountPercent = 0;
}
return discountPercent;
侃侃无极
芜湖不芜
相关分类