Program logic control (1)-sequence structure and branch structure (if statement and switch statement) and the code implementation of their related exercises (determine whether it is a leap year, determine the day of the week, etc.)

Program logic control (1)

1. Sequence structure

  • Is what we often say is output in order
//输出abc
public static void main(String[] args) {
    
    
        System.out.print("a");
        System.out.print("b");
        System.out.print("c");
    }
//输出acb
public static void main(String[] args) {
    
    
        System.out.println("a");
        System.out.println("c");
        System.out.println("b");
    }

2. Branch structure

1. If statement (in the parentheses must be a Boolean expression)

//语法
if(布尔表达式){
    
    
  //条件满足时执行代码
}
//语法
if(布尔表达式){
    
    
  //条件满足时执行代码
}else{
    
    
  //条件不满足时执行代码
}
//语法
if(布尔表达式){
    
    
  //条件满足时执行代码
}else if(布尔表达式){
    
    
  //条件满足时执行代码
}else{
    
    
  //条件都不满足时执行代码
}
  • Determine odd and even numbers and positive and negative numbers
public class TestDemo1 {
    
    
    public static void main(String[] args) {
    
    
        int num = 10;
        if (num % 2 == 1){
    
    
            System.out.println("num是奇数");
        }else{
    
    
            System.out.println("num是偶数");
        }
        
        if (num > 0) {
    
    
            System.out.println("num 是正数");
        } else if (num < 0) {
    
    
            System.out.println("num 是负数");
        } else {
    
    
            System.out.println("num 是 0");
        }
    }
  • Judging Leap Year
public static void main(String[] args) {
    
    
    int year = 2000;
    if (year % 100 == 0) {
    
    
        // 判定世纪闰年
        if (year % 400 == 0) {
    
    
            System.out.println("是闰年");
        } else {
    
    
            System.out.println("不是闰年");
        }
    } else {
    
    
        // 普通闰年
        if (year % 4 == 0) {
    
    
            System.out.println("是闰年");
        } else {
    
    
            System.out.println("不是闰年");
        }
    }
}

2.switch statement

  • Note :
  1. According to the different values ​​in switch, the corresponding case statement will be executed. When a break is encountered, the case statement will end;
  2. If the value in switch does not match the case, the statement in default will be executed;
  3. We recommend that a switch statement should be accompanied by default;
//语法
switch(整数|枚举|字符|字符串){
    
    
case 内容1 : {
    
    
内容满足时执行语句;
[break;]
}
case 内容2 : {
    
    
内容满足时执行语句;
[break;]
}
...
default:{
    
    
内容都不满足时执行语句;
[break;]
}
}
public static void main(String[] args) {
    
    
    int day = 1;
    switch(day) {
    
    
        case 1:
                System.out.println("星期一");
                break;
        case 2:
                System.out.println("星期二");
                break;
        case 3:
                System.out.println("星期三");
                break;
        case 4:
                System.out.println("星期四");
                break;
        case 5:
                System.out.println("星期五");
                break;
        case 6:
                System.out.println("星期六");
                break;
        case 7:
                System.out.println("星期日");
                break;
        default:
                System.out.println("输入有误");
                break;
    }

Guess you like

Origin blog.csdn.net/qq_45665172/article/details/110455598