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

在java中将if-else-if语句转换为switch语句,java,Java,我无法将此程序从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.i

我无法将此程序从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.");
        }

    }


}

您可能需要以下内容:

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.");
}

我敦促您阅读,这应该解释它是如何/为什么工作的。

您可能需要类似以下内容:

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.");
}
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.");
    }

  }

}
我敦促您阅读本手册,该手册应能解释该手册的工作原理

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.");
    }

  }

}
回答:您选择了1


回答:您选择了1。

拜托,您有没有费心去查switch语句是如何工作的?是的,我要去呼应Hatori。这是一个简单的问题,这可能是为什么有这么多快速的答案,但通常在StackOverflow上,你需要展示第一次尝试,并在遇到特定问题时发布。是的,我真的很抱歉我不费吹灰之力的提问。只是这个任务一个小时后就要交了,我不得不去某个地方,所以我担心我没有时间去读它并写一个程序。再说一次,很抱歉这种事不会再发生了。拜托,你有没有费心去查switch语句是如何工作的?是的,我要和Hatori呼应。这是一个简单的问题,这可能是为什么有这么多快速的答案,但通常在StackOverflow上,你需要展示第一次尝试,并在遇到特定问题时发布。是的,我真的很抱歉我不费吹灰之力的提问。只是这个任务一个小时后就要交了,我不得不去某个地方,所以我担心我没有时间去读它并写一个程序。再一次,很抱歉不会再发生了。@Saad没问题,很高兴我能帮上忙。别忘了。是的,我刚刚才认识你see@Saad没问题,很高兴我能帮忙。别忘了。是的,我刚开始做了。正如你所看到的,我对这一点很陌生。你好,桑托什,欢迎来到堆栈溢出:-很高兴看到,你想与其他需要的程序员分享你的编程专业知识。然而,当你帮助别人的时候,准确的回答是非常重要的,只有当你知道自己在做什么的时候才能给出答案。否则,问题很可能会变得更大。遗憾的是,您的答案在编程和逻辑上都不正确。它既不会帮助程序员,也不会帮助该程序的用户按照您建议的方式重新排列代码。您好santhosh,欢迎来到Stack Overflow:-很高兴看到您希望与其他需要的程序员分享您的编程专业知识。然而,当你帮助别人的时候,准确的回答是非常重要的,只有当你知道自己在做什么的时候才能给出答案。否则,问题很可能会变得更大。遗憾的是,您的答案在编程和逻辑上都不正确。它既不会帮助程序员,也不会帮助该程序的用户按照您建议的方式重新排列代码。