Dabai became the ninth day of the Java software siege lion (switch control statement)

Switch control statement in Java

About the switch statement:

1. The witch statement is also a selection structure and also a branch statement

2. The grammatical structure
of the switch statement : a relatively complete switch statement should be written like this

switch (int或String类型的字面值或变量){
    
    
	case int或String类型的字面值或变量:
		java语句;
		java语句;
		...
		break;
	case int或String类型的字面值或变量:
		java语句;
		java语句;
		...
		break;
	case int或String类型的字面值或变量:
		java语句;
		java语句;
		...
		break;
	default:
		java语句;
		...
}

3. The execution principle of the switch statement:

The "data" in the parentheses after the switch statement is matched one by one, and the branches that are successfully matched are matched in order from top to bottom.

4. The branch that matches successfully is executed. If there is a "break;" statement at the end of the branch, the entire switch statement is terminated.

5. The branch that is successfully matched is executed. If there is no "break;" statement in the branch, it will go directly to the next branch for execution (without matching). This phenomenon is called case penetration. [Provide break; sentence can avoid penetration]

case穿透:
int i=10switch(i){
    
    
	case 1:
		java语句;
	case 2:
		java语句;
	case 3: 
		java语句;
		break//输入1,输出的结果是前三条java语句
	case 4:
		java语句;
		System.out.println("Test Code!");
		break;
}

6. All branches are not matched successfully. When there is a default statement, the program in the default branch will be executed

7. After switch and case, only int or String type data can be used, and other types cannot be detected.

  • Of course byte, short, char can also be written directly behind switch and case, because they can be automatically type converted. Byte, short, char can be automatically converted to int type.
  • In JDK6, only the int type can be detected after switch and case.
  • After JDK7, including version 7, introduced new features. The switch keyword and case keyword can detect int and string type data.

8. Cases can be combined:

int i=10switch(i){
    
    
	case 1case 2case 3//输入1、2、3输出的结果相同
		java语句;
		breakcase 4:
		java语句;
		System.out.println("Test Code!");
		break;
}

10. Example of switch statement

public class SwitchTest01
{
    
    
	public static void main(String[] args){
    
    
		long a = 10L;
		int b = a; //编译错误
		
		long x = 10L;
		switch(x){
    
    } //编译报错,switch语句不能用long
		
		解决编译错误:
		long x = 10L;
		switch((int)x){
    
    }
		
		byte b = 10;
		switch(b){
    
    } //编译通过

		short s = 10;
		switch(s){
    
    } //编译通过
		
		char c = 'A';
		switch(c){
    
    } //编译通过
		
		char cc = 12;
		switch(cc){
    
    } //编译通过
		
		 //编译报错
		//switch(ture){}

		String username = "zhangsan";
		switch(username){
    
    }
	}
}

11. Switch can indeed detect the String type, which is a new feature of Java7.

Simple calculator system implementation:

public class SwitchTest02()
{
    
    
	public static void main(String[] args){
    
    
		java.util.Scanner s = new java.util.Scanner(System.in);
		System.out.println("欢迎使用简单计算器系统:");
		System.out.print("请输入第一个数字:");
		int num1 = s.nextInt();
		System.out.print("请输入运算符");
		String operator = s.next();
		System.out.print("请输入第二个数字:");
		int num2 = s.nextInt();

		int result = 0;
		switch(operator){
    
    
			case "+" :
				result = sum1 + sum2;
				break;
			case "-" :
				result = sum1 - sum2;
				break;
			case "*" :
				result = sum1 * sum2;
				break;
			case "/" :
				result = sum1 / sum2;
				break;
			case "%" :
				result = sum1 % sum2;
				break;
			default :
				System.out.println("输入运算符号错误");
		}	
		System.out.println("运算结果为:"+ num1 + operator + num2 + "=" + result );
	}
}

Assuming that the system gives the test taker’s score, judge the test taker’s grade:

  • Range of valid scores: [0-100]
  • Exam scores can have decimals
  • The relationship between test scores and grades:
    [90-100] A
    [80-90] B
    [70-80] C
    [60-70] D
    [0-60] E
  • The above requirements must be completed using the switch statement, not the if
    trick: (int) (score/10)
public class SwitchTest03()
{
    
    
	public static void main(String[] args){
    
    
		double score = 数字:
		int grade = (int)(score/10);
		switch(grade){
    
    
			case 9:case 10:
				System.out.println("A");
				break;
			case 8:
				System.out.println("B");
			case 7:
				System.out.println("C");
			case 6:
				System.out.println("D");
			default:
				System.out.println("E");
		}
	}
}

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112568404