java switch语句_Java Switch语句

java switch语句

In Java, the switch statement is used for executing one statement from multiple conditions. it is similar to an if-else-if ladder.

在Java中,switch语句用于从多个条件执行一个语句。 它类似于if-else-if阶梯。

Switch statement consists of conditional based cases and a default case.

Switch语句由基于条件的案例和默认案例组成。

In a switch statement, the expression can be of byte, short, char and int type.

在switch语句中,表达式可以是byte,short,char和int类型。

From JDK-7, enum, String can also be used in switch cases.

JDK-7开始,枚举也可以在转换案例中使用String

Following are some of the rules while using the switch statement:

以下是使用switch语句时的一些规则:

  1. There can be one or N numbers of cases.

    可以有一个或N个案例。

  2. The values in the case must be unique.

    情况下的值必须唯一。

  3. Each statement of the case can have a break statement. It is optional.

    每个案例的陈述可以有一个break陈述。 它是可选的。

Syntax:

句法:

Following is the syntax to declare the switch case in Java.

以下是在Java中声明切换大小写的语法。

switch(expression)
{    
case value1:    
 			//code for execution;    
 			break;  //optional  
case value2:    
 // code for execution
 break;  //optional  
......    
......
......
......
Case value n:
// code for execution
 break;  //optional  

default:     
 code for execution when none of the case is true;    
}
开关块数据流程图 (Data Flow Diagram Of switch Block)
switch block DFD
示例:使用整数值 (Example: Using integer value)

In this example, we are using int type value to match cases. This example returns day based on the numeric value.

在此示例中,我们使用int类型值来匹配大小写。 本示例根据数值返回天。

public class SwitchDemo1{ 
    public static void main(String[] args) 
    { 
int day = 3; 
        String dayName; 
        switch (day) { 
        case 1: 
dayName = "Today is Monday"; 
            break; 
        case 2: 
dayName = "Today is Tuesday"; 
            break; 
        case 3: 
dayName = "Today is Wednesday"; 
            break; 
        case 4: 
dayName = "Today is Thursday"; 
            break; 
        case 5: 
dayName = "Today is Friday"; 
            break; 
        case 6: 
dayName = "Today is Saturday"; 
            break; 
        case 7: 
dayName = "Today is Sunday"; 
            break; 
        default: 
dayName = "Invalid day"; 
            break; 
        } 
System.out.println(dayName); 
    } 
}
output switch Example
在Switch语句中使用Enum的示例 (Example using Enum in Switch statement)

As we have said, Java allows to use enum in switch cases. So we are creating an enum of vowel alphabets and using its elements in switch case.

如前所述,Java允许在切换情况下使用枚举 。 因此,我们正在创建一个元音字母枚举,并在开关大小写中使用其元素。

public class SwitchDemo2{      
       public enumvowel{a, e, i, o, u}    
       public static void main(String args[])    
       {    
vowel[] character= vowel.values();    
           for (vowel Now : character)    
           {    
                switch (Now)    
                {    
                    case a:    
System.out.println("'a' is a Vowel");    
                        break;    
                    case e:    
System.out.println("'e' is a Vowel");    
                        break;    
                    case i:    
System.out.println("'i' is a Vowel");    
                        break;         
                    case o:    
System.out.println("'o' is a Vowel");    
                        break;    
                    case u:    
System.out.println("'u' is a Vowel");    
                        break;    
                    default:    
System.out.println("It is a consonant");    
                }    
            }    
        }    
}
output-enum-switch program output
示例:开关情况下的字符串 (Example: String in switch case)

Since Java has allowed to use string values in switch cases, so we are using string to create a string based switch case example.

由于Java允许在切换案例中使用字符串值,因此我们在使用string创建基于字符串的切换案例示例。

public static void main(String[] args) {  
    String name = "Mango";  
    switch(name){  
    case "Mango":  
        System.out.println("It is a fruit");  
        break;  
    case "Tomato":  
        System.out.println("It is a vegitable");  
        break;  
    case "Coke":  
        System.out.println("It is cold drink");  
    }  
}  
}

It is a fruit

这是一种水果

示例:无断路开关箱 (Example: without break switch case)

Break statement is used to break the current execution of the program. In switch case, break is used to terminate the switch case execution and transfer control to the outside of switch case.

Break语句用于中断程序的当前执行 。 在开关盒中,break用于终止开关盒执行并将控制权转移到开关盒外部。

Use of break is optional in the switch case. So lets see what happens if we don’t use the break.

在开关盒中break是可选的 。 因此,让我们看看如果不使用中断会发生什么。

public class Demo{      
    
    public static void main(String[] args) {  
        String name = "Mango";  
        switch(name){  
        case "Mango":  
            System.out.println("It is a fruit");  
        case "Tomato":  
            System.out.println("It is a vegitable");  
        case "Coke":  
            System.out.println("It is cold drink");  
        }  
    }
}

It is a fruit It is a vegitable It is cold drink

是水果是蔬菜是冷饮

See, if we don’t use break, it executes all the cases after matching case. 看,如果我们不使用break,它将在匹配大小写之后执行所有情况。

翻译自: https://www.studytonight.com/java/switch-statement.php

java switch语句

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值