4.1 语句的分类

1、顺序语句:不改变语句的执行顺序,按照从上往下,从左往右。

2、分支语句:根据条件分情况执行,只能执行一条语句。if-else;switch

3、循环语句:根据条件让语句反反复复的执行,for;while;do while

4、如何跳出循环:continue;break;return;System.exit(0);

4.2 分支语句

4.2.1 if-else结构

语法1:


if(布尔类型){ 满足条件执行的语句; }


控制台的输入和输出

//导包    快捷键ctrl+shift+o
import java.util.Scanner;
//1、创建一个扫描工具
Scanner input=new Scanner (System.in);
//2、输入提示
System.out.println("请输入一个数字:")
//3、动态接收数据(可多次接受收)
    //整数
int i = input.nextInt();
    //小数
double d = input.nextDouble();
    //串类型
String s = input.next();


示例代码:


System.out.println("欢迎注册信息,请输入您的信息:");
        Scanner  s  =  new  Scanner(System.in);
        
        //提示语
        System.out.println("请输入您的姓名:");
        String  name = s.next();
        System.out.println("请输入您的年龄:");
        int age = s.nextInt();
        System.out.println("请输入您的身高:");
        double height = s.nextDouble();
        
        
        System.out.println("欢迎您,您的信息如下");
        System.out.println(name + "\t" + age + "\t" + height);

结合if 和接收:

//控制台输入
        Scanner sc  = new Scanner(System.in);
        System.out.println("请输入一个数字:");
        int a = sc.nextInt();
        
        // 判断是否是偶数
        if (a % 2 == 0) {
            System.out.println(a + "是偶数");
        }

语法2:(2选1)

if(布尔类型){
        满足条件执行的语句;
    }else{
        不满足条件执行的语句;
    }

注意:else不加条件。

if(age >= 18) {
                    System.out.println("您已成年");
        }else {
                    System.out.println("您还需" + (18 - age) + "成年");
        }

判断闰年:

Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份:");
        
        int year = sc.nextInt();
        if(year % 4 == 0 && year % 100 != 0  ||  year % 400 == 0) {
            System.out.println(year + "是闰年");
        }else {
            System.out.println("不是闰年");
        }

控制台接收整数三个变量,输出他们的最大值(if-else)

public class TestIf_4 {
    public static void main(String[] args) {
        int max = 0;
        if(a > b) {
            max = a;
        }else {
            max = b;
        }
        if(c > max) {
            max = c;
        }
        System.out.println("最大值为:" + max);
        }
    }

语法3:

if(条件1){
        满足条件执行的语句;
    }else if(条件2){
        满足条件执行的语句;
    }else if(条件3){
        满足条件执行的语句;
    }
    ...
    else{
        都不满足执行的语句;
    }

获取最大值:

Scanner sc = new Scanner(System.in);
        System.out.println("请输入三个变量:");
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
    
        int max = 0;
        if(a >= b && a >= c) {
            max = a;
        }else if(b >= a && b >= c) {
            max = b;
        }else {
            max = c;
        }
        System.out.println("最大值:" + max);

4.2.2 switch结构

语法:

switch(表达式或变量){
    case 值1:
        语句1;
    break;
     case 值2:
        语句2;
    break;
    case 值3:
        语句3;
    break;
     defalut:
        都不匹配执行的语句;
     break;   
    }

注意:

1、switch()里的值类型的要求:byte/short/int/char/String(jdk7+)/枚举(jdk5+),不能使用条件。

2、case后面的变量代表和switch里变量匹配一致的,值不能相同。

3、匹配哪个值,执行语句;break表示跳出整个switch,可以不加,但会一次向下执行,直到遇见break。

4、default没有case匹配,执行。

4.3循环语句

4.3.1 循环的定义

当需要反复执行某个代码运行。

4.3.2 循环的分类

for/ while /do-while

4.3.3循环的组成部分

循环的初值:循环的最初状态

循环的条件:能够让循环反复执行的条件

循环体:被反复运行的代码

迭代:循环的变更,每次的变化

4.3.4 for循环

语法:


for(初始值;循环条件;迭代 ){ 循环体; }


循环示例

//打印1-100之间的偶数,并统计个数
        int count=0;
        for (int i=-100;i<=100;i++) {
            if(i%2==0) {
                count++;
                System.out.print(i+"\t");
            }
            if(count%6==0) {
                System.out.println();
            }

打印1-50之间敲七数字,7倍数或以7结尾

for(int x=1;x<=50;x++) {
            if(x%7==0||x%10==7) {
                System.out.println(x);
            }
        }

1-n的累成积,n是控制台接收的

Scanner a=new Scanner (System.in);
            System.out.println("请输入数值:");
            int num =a.nextInt();
            double sum = 1;
            for(int i=1;i<=num;i++) {
                sum*=i;
            }
            System.out.println(sum);
            
        }

统计1900-2022年一共有多少个闰年

int i,count=0;
        for(i=1900;i<=2022;i++) {
            if(i%4==0&&i%100!=0||i%400==0) {
                System.out.print(i+"\t");
                count++;
            
            if(count%4==0) {
                System.out.println();
            }
            }
        }System.out.println("闰年:"+count+"个");