Java 如何自动将if-else if语句转换为switch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28259009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:52:03  来源:igfitidea点击:

How to automatically convert if-else if statement to switch

javaandroidintellij-idea

提问by Daiwik Daarun

Is there a hot key or easy way to replace an if-else if statement with a switch in IntelliJ? I would prefer not to manually convert the if-else if statement to a switch. I am looking to be able to highlight the if-else if statement and press a combination of keys or use a menu button.

是否有热键或简单的方法来用 IntelliJ 中的开关替换 if-else if 语句?我不希望手动将 if-else if 语句转换为 switch。我希望能够突出显示 if-else if 语句并按下组合键或使用菜单按钮。

采纳答案by stkent

Update: as @christopheml pointed out in comments on another answer, "Replace If with Switch" seems to have disappeared in IntelliJ 2018.1. A bug has been filed here.

更新:正如@christopheml 在对另一个答案的评论中指出的那样,“用 Switch 替换 If”似乎在 IntelliJ 2018.1 中消失了。已在此处提交错误。



There are IntelliJ intentions named "Replace If with Switch" and "Replace Switch with If". Check your preferences to see if those options are enabled. If so, I would expect that you would see a quickfix option (Option+Enteron Mac, Alt+Enteron Windows) whenever IntelliJ can automatically handle the conversion from one to the other.

有名为“用开关替换 If”和“用 If 替换开关”的 IntelliJ 意图。检查您的首选项以查看这些选项是否已启用。如果是的话,我希望你会看到一个quickfix选项的(Option+ EnterMac上,Alt+Enter在Windows上)的IntelliJ时可以自动从一个到另一个处理转换。

回答by Henrique de Sousa

I just found out that you have to click on the first if, wait for the light bulb icon to appear and then choose Replace if with Switch.

我刚刚发现你必须先点击if,等待灯泡图标出现,然后选择Replace if with Switch

回答by Henrique de Sousa

When selected if/switch in Eclipse, press Ctrl+1 or right-click -> Quick fix and suggestions will pop-up.

在 Eclipse 中选择 if/switch 时,按 Ctrl+1 或右键单击 -> 快速修复和建议将弹出。

回答by Oded Breiner

Android studio (intelliJ idea):

安卓工作室(智能创意):

Put your cursor on the switch/ifstatement and:

将光标放在switch/if语句上,然后:

mac: option+ return

麦克:option+return

win: alt+ enter

胜利:alt+enter

回答by zgr

If it doesn't offer the replacementanymore, its probably because disabled feature on "Intentions". (Android studio (IntelliJ))

如果它不再提供替代品,可能是因为禁用了“意图”功能。(安卓工作室(IntelliJ))

Solution is simple:

解决方法很简单:

  • File > Settings > Editor > Intentions - Search: "replace switch"
  • 文件 > 设置 > 编辑器 > 意图 - 搜索:“替换开关”

or

或者

  • File > Settings - Search: "replace switch" > Editor > Intentions
  • 文件 > 设置 - 搜索:“替换开关”> 编辑器 > 意图

and check the blank checkbox

并选中空白复选框

回答by kreker

Checkout settings enter image description here

结帐设置 在此处输入图片说明

  • Minimum number of 'if' conditions branches: 3
  • Only suggest on null-safe expressions
  • “if”条件分支的最小数量:3
  • 只建议使用空安全表达式

回答by Ananta Prasad

  1. Select by double clicking on switch keyword
  2. Left Suggestion bulb will appear.
  3. Click on suggestion to replace switch with if else.
  1. 双击 switch 关键字进行选择
  2. 将出现左建议灯泡。
  3. 单击建议以将 switch 替换为 if else。

This works on android studio. It worked in my case

这适用于android studio。它在我的情况下有效