亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將我的枚舉代碼轉換為開關

如何將我的枚舉代碼轉換為開關

智慧大石 2022-06-04 15:02:12
在我的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;
查看完整描述

2 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

您想切換變量 ct


switch(ct) {

        case CustomeType.retail:

            /*Command*/

            break;

        case CustomerType.TRADE:

            /*Command*/

            break;

        default:

            /*else*/

}


查看完整回答
反對 回復 2022-06-04
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

試試這個:(很簡單)


public static double getDiscountPercent(CustomerType ct) {


      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;


   }


查看完整回答
反對 回復 2022-06-04
  • 2 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號