02 |「判断、循环」

前言

判断与循环

一、if 语句

1. 基本 if-else 形式

import java.util.Scanner;

public class Main
{
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    
    if (a > 5) {
        System.out.printf("%d 大于 5", a);
    } else {
        System.out.printf("%d 小于 5", a);
    }
}

2. 简写方式

  • 省略 else 语句
    import java.util.Scanner;
    
    public class Main
    {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        
        if (a > 5) {
            System.out.printf("%d is big", a);
        }
    }
    
  • 省略 { }
    import java.util.Scanner;
    
    public class Main
    {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        
        if (a > 5) 
            System.out.printf("%d is big", a);
        else
            System.out.printf("%d is small", a);
    }
    

3. 嵌套 if-else

import java.util.Scanner;

public class Main
{
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
    
    if (a > b) {
        if (a > c)
            System.out.prinln(a);
        else
            System.out.println(c);
    } else {
        if (b > c)
            System.out.println(b);
        else
            System.out.println(c);
    } 
}

4. 连写 if-else

  • 用来判断某一个值属于哪种情况
  • 本质还是 else 中写 if 语句
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int s = sc.nextInt();

        if (s >= 90) {
            System.out.println("A");
        } else if (s >= 80) {
            System.out.println("B");
        } else if (s >= 60) {
            System.out.println("C");
        } else {
            System.out.println("D");
        }
    }
}

5. 条件表达式

(1)a && b:在满足 a 的情况下,同时需要满足 b;
(2)a || b:a b 两种情况,至少满足一个即可;
(3)!:非

import java.util.Scanner;

public class Main
{
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
    
    if (a >= b && a >= c)  // a最大的情况
        System.out.println(a);
    else if (b >= a && b >= c)  // b最大的情况
        System.out.println(b);
    else
        System.out.println(c);  // c最大的情况
}
if (!(x > 10))
	System.out.printf("%d 不大于 10", x);

二、循环语句

1. 为什么需要循环

  • 生活中的很多逻辑需要进行重复操作。

2. 理解循环的关键

  • 找到循环的内容;
  • 代码的循环顺序;

3. 分类

1)while 循环
  • 本质为循环版的 if 判断。区别在于 if 只判断一次(如果成立,就执行判断内容,不成立就退出);while 是判断多次;
2)for 循环
  • 基本结构
for (init-statement; condition; expression) {
	   statement
}
  • 其它结构
init-statement可以定义多个变量
expression也可以修改多个变量
for (int i = 1, j = 10; i < j; i ++, j -- ) {
     sum += i * j;
}
  • 多层循环
4. 跳转语句
1) break
  • 提前从循环中退出,一般与 if 语句搭配;
2)continue
  • 直接跳到当前循环体的结尾;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

工科男小Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值