【0基础学Java第四课】-- 逻辑控制

4.1 顺序结构

按照代码书写的顺序一行一行执行。

4.2 分支结构

4.2.1 if语句

  1. 语法格式1
    在这里插入图片描述

  2. 语法格式2
    在这里插入图片描述

  3. 语法格式3
    在这里插入图片描述`

判断一个数字是奇数还是偶数

import java.util.Scanner;
    public static void main3(String[] args) {

        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt(); // 输入一个整数

        if (num % 2 == 0) {
            System.out.println(num+ " 是偶数");
        }else {
            System.out.println(num+ "是奇数");
        }
    }

判断一个数字是正数,负数,还是零

    public static void main4(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();

        if (num > 0) {
            System.out.println("正数");
        }else if(num < 0) {
            System.out.println("负数");
        }else {
            System.out.println("0");
        }
    }

判断一个年份是否为闰年

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int year = scan.nextInt();
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            System.out.println(year +"闰年");
        } else {
            System.out.println(year +"不是闰年");
        }
    }

注意

  • 代码风格(Java代码的风格走)
    在这里插入图片描述
  • 分号问题
    public static void main(String[] args) {
        int x = 20;
        if (x == 10);
        {
            System.out.println("hehe");
        } // 运行结果hehe
        //此处多写了一个 分号, 导致分号成为了 if 语句的语句体,
        // 而 { } 中的代码已经成为了和一个 if 无关的代码块.
    }

4.2.2 switch 语句

执行流程

  1. 先计算表达式的值
  2. 和case依次比较,一旦有响应的匹配就执行该项下的语句,直到遇到break时结束
  3. 当表达式的值没有与所列项匹配时,执行default
    public static void main2(String[] args) {
        int a = 1;
        //switch 参数里面不能是一个很复杂的参数
        switch (a){
            case 1:
                System.out.println("1");
                //break;
            case 2:
                System.out.println("2");
                break;
            default:
                System.out.println("sss");
                break;
        }

        String str = "abc";
        switch (str){
            case "abc":
                System.out.println("abc");
                break;
            case "124":
                System.out.println("124");
                break;
            default:
                System.out.println("输入有误");
                break;
        }

        //可以是枚举

        //面试题: 不能作为switch参数的数据类型是什么?
        // float double boolean long\
        char ch = 'a';
        switch (ch){

        }
    }

注意

  1. 多个case后的常量值不可以重复
  2. switch的括号内只能是以下类型的表达式:
  • 本类型:byte、char、short、int,注意不能是long类型
  • 引用类型:String常量串、枚举类型

4.3 while循环

基本语法格式
在这里插入图片描述

打印 1 - 10 的数字

    public static void main(String[] args) {
        //打印 1 - 10 的数字
        int a = 1;
        while(a <= 10) {
            System.out.println(a);
            a++;
        }
    }

计算 1 - 100 的和

    public static void main(String[] args) {
        int a = 1;
        int sum = 0;
        while(a <= 100) {
            sum += a;
            a++;
        }
        System.out.println(sum);
    }

计算 5 的阶乘

    public static void main(String[] args) {
        //计算 5 的阶乘
        int i = 1;
        int ret = 1;
        while(i <= 5) {
            ret *= i;
            i++;
        }
        System.out.println("5的阶乘:"+ret);
    }

计算1!+2!+ 3!+ 4!+ 5!

    public static void main(String[] args) {
        //计算1!+2!+ 3!+ 4!+ 5!
        int n = 1;
        int sum = 0;
        // 外层循环负责求阶乘的和
        while (n <= 5){
            int ret = 1;
            int i= 1;
            //里层循环负责完成求阶乘的细节
            while (i <= n) {
                ret *= i;
                i++;
            }
            sum += ret;
            n++;
        }
        System.out.println(sum);
    }

这里我们发现, 当一个代码中带有多重循环的时候, 代码的复杂程度就大大提高了. 而比较复杂的代码就更容易出错.后面我们会采用更简单的办法来解决这个问题。
注意

  1. 和 if 类似, while 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
  2. 和 if 类似, while 后面的 { 建议和 while 写在同一行.
  3. 和 if 类似, while 后面不要多写 分号, 否则可能导致循环不能正确执行.

4.4 break

break 的功能是让循环提前结束.
例题:找到 100 - 200 中第一个 3 的倍数

    public static void main(String[] args) {
        // 100 - 200 中第一个 3 的倍数
        int num = 100;
        while (num <= 200) {
            if (num % 3 == 0) {
                System.out.println("找到了 3 的倍数, 为:" + num);
                break;
            }
            num++;
        }
    }

执行到break,退出循环。

4.5 continue

continue 的功能是跳过这次循环, 立即进入下次循环.
例题:找到 100 - 200 中所有 3 的倍数

    public static void main(String[] args) {
        int num = 100;
        while (num <= 200) {
            if (num % 3 != 0) {
                num++;
                continue;
            }
            System.out.println("3的倍数:"+num);
            num++;
        }
    }

执行到 continue 语句的时候, 就会立刻进入下次循环(判定循环条件), 从而不会执行到下方的打印语句.

4.6 for循环

for(表达式①;布尔表达式②;表达式③){
表达式④;
}

  • 表达式1: 用于初始化循环变量初始值设置,在循环最开始时执行,且只执行一次
  • 表达式2: 循环条件,满则循环继续,否则循环结束
  • 表达式3: 循环变量更新方式

打印1-10的数字

    public static void main(String[] args) {
        for (int i = 1;i <= 10;i++) {
            System.out.println(i);
        }
    }

计算 1 - 100 的和

    public static void main(String[] args) {
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            sum += i;
        }
        System.out.println("sum = "+sum);
    }

计算 5 的阶乘

    public static void main(String[] args) {
        int ret = 1;
        for (int i = 1; i < 6; i++) {
            ret *= i;
        }
        System.out.println("5的阶乘:"+ret);
    }

计算 1! + 2! + 3! + 4! + 5!

    public static void main(String[] args) {
        int sum = 0;
        for (int j = 1; j <= 5; j++) {
            int ret = 1;
            for (int i = 1; i < 6; i++) {
                ret *= i;
            }
            sum += ret;
        }
        System.out.println("1-5的阶乘和:"+sum);
    }

注意

  1. 和 if 类似, for 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
  2. 和 if 类似, for 后面的 { 建议和 while 写在同一行.
  3. 和 if 类似, for 后面不要多写 分号, 否则可能导致循环不能正确执行.
  4. 和while循环一样,结束单趟循环用continue,结束整个循环用break

4.7 do while 循环

do{
循环语句;
}while(循环条件);
先执行循环语句, 再判定循环条件,循环条件成立则继续执行,否则循环结束。
比如:打印1-10

    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.println(i);
            i++;
        } while (i <= 10);
    }

注意

  1. do while 循环最后的分号不要忘记
  2. 一般 do while 很少用到, 更推荐使用 for 和 while.

4.8 输入输出

4.8.1 输出到控制台

    public static void main(String[] args) {
        System.out.println("输出换行"); 
        System.out.print("输出不带换行"); 
        System.out.printf("%s\n", "格式化输出"); 
    }
  • println 输出的内容自带 \n, print 不带 \n
  • printf 的格式化输出方式和 C 语言的 printf 是基本一致的.

4.8.2 从键盘输入

使用 Scanner 读取字符串/整数/浮点数

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name = sc.nextLine();
        System.out.println("请输入你的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入你的工资:");
        float salary = sc.nextFloat();
        System.out.println("你的信息如下:");
        System.out.println("姓名: "+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary);
        sc.close(); // 注意, 要记得调用关闭方法
    }

使用 Scanner 循环读取 N 个数字

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int a = sc.nextInt();
            System.out.println(a);
        }
    }

使用 Scanner 循环读取 N 个数字,并求取其平均值

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        int num = 0;
        while (sc.hasNextInt()) {
            int tmp = sc.nextInt();
            sum += tmp;
            num++;
        }
        System.out.println("sum="+sum);
        System.out.println("avg="+sum/num);
        sc.close();
    }

注意
当循环输入多个数据的时候, 使用 ctrl + z 来结束输入 (Windows 上使用 ctrl + z, Linux / Mac 上使用 ctrl+ d)。

  • 15
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值