Java_009 switch selection statement, continue statement

switch selection statement: Switch, conversion
Syntax description:

  • The type of expression can only be byte, short, char, and int.
  • The value n can only be a constant or a constant, not a variable.
  • Any number of sentences can be written in the function code part.
  • The break keyword means to break, which means the end of the switch statement. The break statement is optional.
  • The case statement can have any number of sentences, which are labeled statements.
  • The default statement can be written anywhere in the switch, and its function is similar to the else in the if statement.

The following is an example of the switch statement code that obtains the number of days in each month based on the month (without considering leap years):

import java.util.Scanner;


public class example06 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);/新建一个sannner来获取用户输入
        System.out.println("请输入月份整数:");
        int month = scanner.nextInt();
        int days = 0;

        switch (month){
    
    
            case 1:
                days = 31;
                break;
            case 2:
                days = 28;
                break;
            case 3:
                days = 31;
                break;
            case 4:
                days = 30;
                break;
            case 5:
                days = 31;
                break;
            case 6:
                days = 30;
                break;
            case 7:
                days = 31;
                break;
            case 8:
                days = 31;
                break;
            case 9:
                days = 30;
                break;
            case 10:
                days = 31;
                break;
            case 11:
                days = 30;
                break;
            case 12:
                days = 31;
                break;
        }
        scanner.close();
        System.out.println(month + "月的天数是" + days + "天");
    }
}

The code block in the above code can be simplified to:

public class example06_01 {
    
    
    public static void main(String[] args) {
    
    
        int monnth_01= 10;
        int days = 0;
        switch (monnth_01){
    
    
            case 2:
                days = 28;
                break;
            case 4://条件可以写在一起
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            default:
                days = 31;
        }
        System.out.println(days);

    }
}

note:

  • Switch is not very suitable for interval discrimination, and more interval discrimination is generally realized by using if-elseif statements.
  • The if statement can realize all the conditions in the program. The switch statement is particularly suitable for judging the equality of a series of points. The structure is also clearer, and the execution speed is slightly faster than the if statement. In the actual code, you can use the corresponding one as needed. Statement to realize the logic function required by the program.

Code block analysis:

        int score = 87;
      switch (score / 10){
    
    //得出十位上的数值
          case 9:
              System.out.println('A');
              break;
          case 8:
              System.out.println('B');
              break;
          case 7:
              System.out.println('C');
              break;
          case 6:
              System.out.println('D');
              break;

          default:
              System.out.println('E');
      }

appendix:

  • The continue statement can only be inside the loop statement, and the function is to skip the loop and continue to execute the next loop structure.
  • In the while and do...while statements, the continue statement jumps to the loop condition to continue execution, and in the for statement the continue statement jumps to the iteration statement to continue execution.
    The sample code is as follows:
  int i = 0;
        while (i < 4);
        i ++;
        if (i == 2) {
    
    
            continue;
        }
        System.out.println(i);
    }

At last

  1. Tips on defining labeling principles:For Java, the only place where a label is used is before the loop statement. It actually needs to be immediately before the loop statement-it is unwise to put any statement between the label and the loop.
  2. And the only reason to set the label before the loop is:We want to nest another loop or a switch in it. This is because the break and continue keywords usually only interrupt the current loop, but if used with tags, they will interrupt to the place where the tag exists.

Guess you like

Origin blog.csdn.net/weixin_49207937/article/details/114547542