The article directories

  • Switch branching structure
    • 1. Basic grammar
    • Flow chart of 2.
    • 3. Quick start
    • 4. Switch notes and details discussion
    • 5. Switch classes
    • 6. Comparison between switch and IF

Switch branching structure

1. Basic grammar

Flow chart of 2.

  1. Draw the swtich out flow
  2. Case Description Flow chart

3. Quick start

  • Java please write a program, this program can receive a character, such as: A, B, C, D, E, F, G A means Monday, B means Tuesday… Displays information based on user input. To complete the code, use the switch statement:
	/* Example: switch01.java Write a program that can accept a character, such as: A,b, C, D,e,f,g A for Monday,b for Tuesday... Displays information based on user input. 1. Receive a character and create a Scanner object. 2. Use switch to complete the match and print the corresponding information code */
	Scanner myScanner = new Scanner(System.in);
	System.out.println("Please enter one character (A-g)");
	char c1 = myScanner.next().charAt(0);//
	// In Java, any expression that returns a value is an expression
	switch(c1) {
		case 'a' : 
			System.out.println("It's Monday. The monkey wears new clothes.");
			break;
		case 'b' : 
			System.out.println("It's Tuesday. Monkey's the mistress.");
			break;
		case 'c' : 
			System.out.println("Today is Wednesday, monkey climb snow mountain..");
			break;
		/ /...
		default:
			System.out.println("The characters you typed are not correct. There are no matches.");
	
	}
	
	System.out.println("Exit the switch and continue executing the program.");
Copy the code

4. Switch notes and details discussion

	/ / 1 for details
	// The expression data type should be the same as the constant type after case,
	// Or it can be automatically converted to a type that can be compared, such as a character and a constant int
	
	2 / / details
	// The return value of the expression in switch(expression) must be:
	/ / (byte, short, int, char, enum] [enumeration, String)

	3 / / details
	// The value in the case clause must be a constant (1,'a') or a constant expression, not a variable
	//
	/ / 4 for details
	// The default clause is optional. When no case matches, default is executed
	// If there is no default clause and no constant is matched, there is no output
	//
	5 / / details
	// The break statement is used to break out of the switch block after executing a case branch;
	// If no break is written, the program will execute sequentially to the end of the switch, unless break is executed
	char c = 'b';
	char c2 = 'c';
	switch(c) {
		case 'a' :
			System.out.println("ok1");
			break;
		case 'b'  :
			System.out.println("ok2");
			break;
		default :
			System.out.println("ok3");
	}

	System.out.println("Exit switch, continue execution...");
Copy the code

  • If break is removed from case B, the following statement will be executed.

5. Switch classes

SwitchExercise.java

  1. Converts a char from lowercase to uppercase (keyboard input) using switch. Convert only a, B, C, D, e. Output “other”.
	// Use switch to change the lowercase type
	// Char is uppercase (keyboard input). Only convert a->A, b->B, C, D, e.
	// Output "other".
	Scanner sc = new Scanner(System.in);
	System.out.println("Please enter a-E");
	char c1 = sc.next().charAt(0);
	switch(c1) {
		case 'a' :
			System.out.println("A");
			break;
		case 'b' :
			System.out.println("B");
			break;
		case 'c' :
			System.out.println("C");
			break;
		case 'd' :
			System.out.println("D");
			break;
		case 'e' :
			System.out.println("E");
			break;
		default :
			System.out.println("Your input is wrong.");
	
	}
Copy the code

  1. Students with a score greater than 60 will be output as “qualified”. Below 60 points, output “unqualified”. (Note: the input grade cannot be greater than 100), hint: grade /60
	// For students with a score greater than 60, output "pass". Under 60 points,
	// Output "unqualified". (Note: the input score cannot be greater than 100), prompt score /60
	//
	//1. This problem can be done with branches, but requires switch
	//2. Here we need to make a transformation, programming idea:
	// If the result is [60,100], (int)(result /60) = 1
	// If the result is [0,60], (int)(result /60) = 0
	
	// Code implementation
	Scanner sc = new Scanner(System.in);
	System.out.println("Please enter score between 0 and 100:");
	double score = sc.nextDouble();
	if (score >= 0 && score<=100) {
	    switch ((int) (score / 60)) {
	        case 0:
	            System.out.println("Unqualified");
	            break;
	        case 1:
	            System.out.println("Qualified"); }}else{
	    System.out.println("Please enter a score between 0 and 100");
	}
Copy the code

  1. Prints the season for the month used for the specified month. 3,4,5 spring 6,7,8 summer 9,10,11 fall 12, 1, 2 winter [classroom exercise, hint: use penetration]
	// According to the month used to specify,
	// Prints the season of the month.
	//3,4,5 spring 6,7,8 summer 9,10,11 autumn 12, 1, 2 winter
	//[class exercise, hint to use penetration]
	//
	//
	//1. Create a Scanner object to receive user input
	//2. Use int month to receive messages
	//3. Use switch to match, use penetration to complete, relatively concise
	
	Scanner myScanner = new Scanner(System.in);
	System.out.println("Input month");
	int month = myScanner.nextInt();
	switch(month) {
		case 3:
		case 4:
		case 5: 
			System.out.println("It's spring.");
			break;
		case 6:
		case 7:
		case 8: 
			System.out.println("It's summer.");
			break;
		case 9:
		case 10:
		case 11: 
			System.out.println("This is autumn.");
			break;
		case 1:
		case 2:
		case 12: 
			System.out.println("It's winter.");
			break;
		default :
			System.out.println("You entered the wrong month (1-12)");
	}
Copy the code

6. Comparison between switch and IF

  1. If the specific value of the judgment is not much, and consistentByte, short, int, char, enum, StringThese six types. Although both statements can be used, it is recommendedswtichStatements.
  2. Other cases: For interval judgment, the result isbooleanType determination, useif.ifIs more widely used.