Android应用集成library模块遇switch-case错误,请求转为if-else代码
没问题,我来帮你把switch-case语句转换成if-else语句!先给你举几个常见的Android场景下的示例,你可以对照着修改自己的代码:
示例1:基于整数的switch-case转if-else
原switch-case代码:
int status = getCurrentStatus(); switch(status) { case 0: Toast.makeText(this, "状态:未启动", Toast.LENGTH_SHORT).show(); break; case 1: startDataLoading(); break; case 2: showCompletedUI(); break; default: showUnknownStatusUI(); }
转换后的if-else代码:
int status = getCurrentStatus(); if (status == 0) { Toast.makeText(this, "状态:未启动", Toast.LENGTH_SHORT).show(); } else if (status == 1) { startDataLoading(); } else if (status == 2) { showCompletedUI(); } else { showUnknownStatusUI(); }
示例2:基于字符串的switch-case转if-else
原switch-case代码:
String mediaType = getSelectedMediaType(); switch(mediaType) { case "VIDEO": launchVideoPlayer(); break; case "AUDIO": initAudioPlayer(); break; default: showUnsupportedMediaTip(); }
转换后的if-else代码:
String mediaType = getSelectedMediaType(); if ("VIDEO".equals(mediaType)) { launchVideoPlayer(); } else if ("AUDIO".equals(mediaType)) { initAudioPlayer(); } else { showUnsupportedMediaTip(); }
如果你的代码有更特殊的逻辑(比如带条件判断的case、枚举类型等),可以把具体代码贴出来,我再帮你精准转换~
内容的提问来源于stack exchange,提问作者hidayat thoyib
相关产品推荐
相关产品推荐

