Java switch 语句:If Else 条件允许我们在 TRUE 或 FALSE 之间进行选择。当有多个选项时,我们只需使用嵌套 If。假设,如果我们有十个备选项呢?如果在此情况下使用嵌套 If,那么编程逻辑将难以理解。
此编程中的 Else if 和 switch 语句可以有效地处理这些问题。我们在上一篇文章中讨论了 Else If 语句,所以这里让我们探讨 switch case。
Java 编程中 switch case 的工作功能与 If 条件几乎相同。如前所述,它可能有 n 个 case。因此,它将表达式值与 case 块中赋的值进行比较。
如果表达式值和 case 值都匹配,则将执行该 switch case 语句中的代码块。让我们来看一下它的语法以便更好地理解。
Java switch case 语法
此编程语言中 switch 语句的语法如下
Switch (expression) {
Case Option 1:
//Execute when the expression result match Option 1
break;
Case Option 2:
//Execute when the expression result match Option 2
break;
......
Case Option N:
//Execute these when the result of expression match Option N
break;
Default:
/*Execute these when the result of expression
Not matching with any Option */
brea;
}
此 switch case 语句语法背后的详细解释是
- 表达式的值应为整数或字符(我们也可以将表达式写为 n/2,但结果应为整数或可转换的整数)。
- 它允许我们添加一个默认语句。如果变量值与任何 switch case 表达式都不匹配,则将执行可选默认语句中的代码。
- break 子句用于退出。否则,条件中的所有代码都将被执行。每当遇到 break 时,执行流将直接跳出 case。请参考 Break Statement 文章以完美理解此关键字。
Java switch case 流程图
下图将向您展示此 switch case 语句背后的流程图。

switch 语句或 case 的执行流程是
- 如果 Case = Option 1,则执行 STATEMENT1,然后执行 break 语句退出。
- 如果 Case = Option 2,则执行 STATEMENT2,然后执行 break 退出。
- 当 Case = Option 3 时,执行 STATEMENT3,然后执行语句退出。
- 如果 Option 1、Option 2 和 Option 3 失败,则执行 Default STATEMENT,然后执行 break 语句退出。
Java switch case 示例
此 程序 允许用户输入两个整数值。它还允许选择任何 算术运算符 来执行算术运算。
在此 Java switch case 示例中,前三行代码包含 println,允许用户输入两个整数值。然后我们将用户输入的值赋给已声明的变量 number1 和 number2。
下一个 println 将允许用户输入一个字符(应为任何算术运算符)。然后我们将用户输入的字符赋给已声明的变量 operator。
接下来,我们使用 operator 作为选项的 switch case 语句。如果用户输入的 operator 是 +,将打印 + 块内的代码。
如果用户输入 – 作为运算符,将打印下面的语句。当用户输入 * 时,将打印下面的代码。
如果用户输入 / 作为运算符,将打印 / 部分内的代码。如果用户提供 % 作为运算符,将打印 % 部分内的 println。
如果用户输入的运算符(字符)不在以上任何一个中(运算符不等于 +、-、*、/ 或 %),将打印 Java switch case 中的以下默认语句。
请参考 Java 中的 If Condition、If Else、Else If 和 Nested If 文章。
package ConditionalStatements;
import java.util.Scanner;
public class Switch {
private static Scanner sc;
public static void main(String[] args) {
int number1, number2;
char operator;
sc = new Scanner(System.in);
System.out.println(" Please Enter two values to perform Arithmetic Operations ");
number1 = sc.nextInt();
number2 = sc.nextInt();
System.out.println(" Please select any ARITHMETIC OPERATOR You wish!\n");
operator = sc.next().charAt(0);
switch (operator) {
case '+':
System.out.format("Addition of two numbers is: %d", number1 + number2);
break;
case '-':
System.out.format("Subtraction of two numbers is: %d", number1 - number2);
break;
case '*':
System.out.format("Multiplication of two numbers is: %d", number1 * number2);
break;
case '/':
System.out.format("Division of two numbers is: %d", number1 / number2);
break;
case '%':
System.out.format("Module of two numbers is: %d", number1 % number2);
break;
default:
System.out.println("You have entered the Wrong operator\n");
System.out.println("Please enter the Correct operator such as +, -, *, /, %%");
break;
}
}
}
输出 1:让我们输入 * 运算符作为 switch case 表达式值,并输入 number1 = 10,number 2 = 20。

让我们输入错误的运算符来检查默认值
Please Enter two values to perform Arithmetic Operations
40 60
Please select any ARITHMETIC OPERATOR You wish!
6
You have entered the Wrong operator
Please enter the Correct operator such as +, -, *, /, %%
switch case 字符串示例
在此 程序 中,我们使用字符串数据作为选项。在此 Java switch case 示例中,我们使用面向对象编程来划分代码。首先,我们将创建一个包含执行此操作的方法的类。
package ConditionalStatements;
import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args) {
String month, Message;
sc = new Scanner(System.in);
System.out.println("Please Enter any Month Name");
month = sc.next();
MonthClass swt = new MonthClass();
Message = swt.MonthNum(month);
System.out.println(Message);
}
}
在此 switch case 示例的主程序中,我们将创建上述类的实例并调用方法。
package ConditionalStatements;
public class MonthClass {
public String MonthNum(String month) {
String Message;
switch (month.toLowerCase()) {
case "january":
Message = "You have entered January and Month Number = 1";
break;
case "february":
Message = "You have entered February and Month Number = 2";
break;
case "march":
Message = "You have entered March and Month Number = 3";
break;
case "april":
Message = "You have entered April and Month Number = 4";
break;
case "may":
Message = "You have entered May and Month Number = 5";
break;
case "june":
Message = "You have entered June and Month Number = 6";
break;
case "july":
Message = "You have entered July and Month Number = 7";
break;
case "august":
Message = "You have entered August and Month Number = 8";
break;
case "september":
Message = "You have given September and Month Number = 9";
break;
case "october":
Message = "You have given October and Month Number = 10";
break;
case "november":
Message = "You have given November and Month Number = 11";
break;
case "december":
Message = "You have given December and Month Number = 12";
break;
default:
Message = "Please Provide the Proper Month name";
break;
}
return Message;
}
}
输出 1:让我们输入三月作为月份名称。
Please Enter any Month Name
march
You have entered March and Month Number = 3
让我们输入错误的月份名称来检查默认值。
Please Enter any Month Name
mar
Please Provide the Proper Month name