java中将if-else-if语句转换为switch语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19040706/
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-12 13:36:01  来源:igfitidea点击:

Converting an if-else-if statement into a switch statement in java

java

提问by zaynv

I'm having trouble turning this program from an if-else-if statement into a switch statement. Any help would be appreciated.

我无法将此程序从 if-else-if 语句转换为 switch 语句。任何帮助,将不胜感激。

import java.util.Scanner;

public class ifToSwitchConversion {


    public static void main(String [] args) {

        // Declare a Scanner and a choice variable
        Scanner stdin = new Scanner(System.in);
        int choice = 0;

        System.out.println("Please enter your choice (1-4): ");
        choice = stdin.nextInt();

        if(choice == 1)
        {
            System.out.println("You selected 1.");
        }
        else if(choice == 2 || choice == 3)
        {
            System.out.println("You selected 2 or 3.");
        }
        else if(choice == 4)
        {
            System.out.println("You selected 4.");
        }
        else
        {
            System.out.println("Please enter a choice between 1-4.");
        }

    }


}

采纳答案by MeIr

import java.util.Scanner;

public class ifToSwitchConversion {

public static void main(String [] args) {

    // Declare a Scanner and a choice variable
    Scanner stdin = new Scanner(System.in);
    int choice = 0;

    System.out.println("Please enter your choice (1-4): ");
    choice = stdin.nextInt();


    switch(choice) {
        case 1:
            System.out.println("You selected 1.");
            break;
        case 2:
        case 3:
            System.out.println("You selected 2 or 3.");
            break;
        case 4:
            System.out.println("You selected 4.");
            break;
        default:
            System.out.println("Please enter a choice between 1-4.");
    }

  }

}

回答by Idan Arye

switch(choice)
{
    case 1:
        System.out.println("You selected 1.");
        break;
    case 2:
    case 3:
        System.out.println("You selected 2 or 3.");
        break;
    case 4:
        System.out.println("You selected 4.");
        break;
    default:
        System.out.println("Please enter a choice between 1-4.");
}

回答by arshajii

You probably want something like:

你可能想要这样的东西:

switch (choice) {
    case 1:
        System.out.println("You selected 1.");
        break;
    case 2:
    case 3:  // fall through
        System.out.println("You selected 2 or 3.");
        break;
    case 4:
        System.out.println("You selected 4.");
        break;
    default:
        System.out.println("Please enter a choice between 1-4.");
}

I urge you to read the switch statement tutorial, which should explain how/why this works as it does.

我敦促你阅读switch 语句教程,它应该解释它是如何/为什么这样做的。

回答by santhosh

/* Just change choice to 1
 * if you want 2 or 3 or 4
 * just change the switch(2 or 3 or 4) 
 */

switch(1)
{
    case 1:
        System.out.println("You selected 1.");
        break;
    case 2:
    case 3:
        System.out.println("You selected 2 or 3.");
        break;
    case 4:
        System.out.println("You selected 4.");
        break;
    default:
        System.out.println("Please enter a choice between 1-4.");
}

Answer : You selected 1.

答案:您选择了 1。