【判断和循环】


一、流程控制语句——顺序结构

通过一些语句,控制程序的执行流程

  1. 顺序结构
  2. 分支结构
  3. 循环结构

顺序结构:是Java程序默认的执行流程,按照代码的先后顺序,从上到下依次执行

二、分支结构

  • if
  • switch

if语句

第一种格式

if (关系表达式){
语句体;
}

注意点:

  1. 在大括号的开头可以另起一行,但是建议写在第一行的末尾
  2. 在语句体中,如果只有一句代码,大括号可以省略
  3. 如果对一个布尔类型的变量进行判断,不要用==,直接把变量写在小括号里即可

小练习:
红绿灯练习

public class test1 {
    
    
    public static void main(String[] args) {
    
    
        //红灯停,绿灯行,黄灯缓
        //1.定义三个变量
        boolean isLightGreen = true;
        boolean isLightYellow = false;
        boolean isLightRed = false;

        //2.进行判断
        if(isLightGreen){
    
    
            System.out.println("GoGoGo!!!");
        }
        if(isLightYellow){
    
    
            System.out.println("slow!");
        }
        if(isLightRed){
    
    
            System.out.println("stop!!!");
        }

    }
}// GoGoGo!!!

第二种格式

if (关系表达式){
语句体;
}else{
语句体2;
}

小练习:

import java.util.Scanner;

//商品付款
public class test1{
    
    
    public static void main(String[] args) {
    
    
        //一个商品600元,如果付款大于600,显示付款成功,小于600,付款失败
        Scanner sc = new  Scanner(System.in);
        System.out.println("请输入付款金额:");
        int money = sc.nextInt();
        if(money>=600){
    
    
            System.out.println("付款成功");
        }else{
    
    
            System.out.println("付款失败");
        }
    }
}

第三种格式

if (关系表达式){
语句体;
}else if{
语句体2;
}

else{
语句n+1;
}
练习:

import java.util.Scanner;

//成绩
public class test1{
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("小明成绩:");
        int score = sc.nextInt();
        if(score >=0 && score <=100) {
    
    
            if (score >= 95 && score <= 100) {
    
    
                System.out.println("自行车");
            } else if (score >= 90 && score <= 94) {
    
    
                System.out.println("游乐场");
            } else if (score >= 80 && score <= 89) {
    
    
                System.out.println("变形金刚");
            } else {
    
    
                System.out.println("挨揍");
            }
        }else{
    
    
            System.out.println("输入错误");
        }

    }
}

总结:
第一种格式:适合单条件判断
第二种格式:适合双条件判断
第三种格式:适合多条件判断

switch语句

语句格式:
在这里插入图片描述
注意:

  1. case后面的值只能是字面量,不能是变量。
  2. case的值不能重复。

练习:

import java.util.Scanner;

//减肥计划
public class test1{
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("输入星期几:");
        int week = sc.nextInt();
        switch(week){
    
    
            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;
        }
    }
}

switch其他特性

  • default的位置和省略
  • case穿透
  • switch新特性
  • switch和if各自的使用场景

default的位置和省略

  1. 位置:default不一定写在最下面,可以写在任意位置,但是习惯写在最下面
  2. 省略:default可以省略,语法没有任何问题,不建议省略

case穿透
语句体中缺少break,导致case穿透
在执行过程中,如果发现了break,则结束switch语句的执行,如果没有发现break语句,那么程序就会继续执行下一个case语句,直到遇到break或者右大括号

switch新特性
针对jdk12
利用大括号简化了break
在这里插入图片描述

switch和if的第三种格式各自的使用场景

  • if的第三种格式:一般用于对范围的判断
  • switch:把有限个数据一 一列举出来,让我们任选其一
import java.util.Scanner;

//判断是休息日还是工作日
public class test1{
    
    
    public static void main(String[] args) {
    
    
        //定义变量
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入星期几:");
        int week = sc.nextInt();
        switch (week){
    
    
            case 1:
            case 2:
            case 3:
            case 4:  //利用case穿透
            case 5:
                System.out.println("工作日");
                break;
            case 6:
            case 7:
                System.out.println("休息日");
                break;
            default:
                System.out.println("输入错误");
                break;

        }



    }
}

还可以简化成:

 switch (week){
    
    
            case 1,2,3,4,5:
                System.out.println("工作日");
                break;
            case 6,7:
                System.out.println("休息日");
                break;
            default:
                System.out.println("输入错误");
                break;

        }

三、循环语句

循环分类:

  • for
  • while
  • do…while

for循环

在这里插入图片描述
练习:

//打印1-5,再倒着打印5-1

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

        for(int i = 5;i >= 1;i--){
    
    
            System.out.println(i);
        }

    }
}

累加思想:

//累加1-5
public class test1 {
    
    
    public static void main(String[] args) {
    
    
        //1.取出1-5的每一个数

        //2.将1-5的和赋值给一个变量
        int sum = 0;
        for(int i = 1; i <= 5; i++){
    
    
            System.out.println(i);
            sum += i;
        }
        System.out.println(sum);
    }
}

求1-100的偶数和

//打印1-100的偶数和
public class test1 {
    
    
    public static void main(String[] args) {
    
    

        int sum = 0;
        //1.获取1-100
        for (int i = 1; i <= 100; i++) {
    
    

            //2.判断
            if(i % 2 == 0){
    
    
                sum += i;
            }
        }
        System.out.println(sum);
    }
}

统计满足条件的数的个数

import java.util.Scanner;
//统计满足条件的数字
//键盘录入两个数字,在这两个数字区间内,既能被3整除,又能被5整除
public class test1 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("输入开始的数字:");
        int start = sc.nextInt();
        System.out.println("结束的数字:");
        int end = sc.nextInt();

        int count = 0;
        for (int i = start; i <= end; i++) {
    
    
            //判断是否满足条件
            if(i % 3 == 0 && i % 5 == 0){
    
    
                count++;
            }
        }
        System.out.println(count);
    }
}

while循环

在这里插入图片描述
利用while循环打印1-100

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

for和while的区别

  • for循环中:知道循环次数或者循环范围
  • while循环:不知道循环的次数和范围,只知道循环的结束条件

统计折纸次数:

//打印折纸次数
//一张纸0.1mm厚,问这多少次才可以折成珠穆朗玛峰的高度?
//(珠穆朗玛峰,8844.43m=8844430mm)

public class test1 {
    
    
    public static void main(String[] args) {
    
    
        //1.定义纸张厚度、珠穆朗玛峰高度、统计次数
        double paper = 0.1;
        double hight = 8844430;
        int count = 0;
        //while循环
        while (paper < hight){
    
    
            paper *= 2;
            count++;
        }
        System.out.println(count);
    }
}

判断是否是回文数:

//回文数,判断是否为回文数
public class test1 {
    
    
    public static void main(String[] args) {
    
    
        //定义数字
        int x = 121;
        //定义一个数用来记录x的初始值
        int temp = x;
        //定义倒过来的数字
        int num = 0;
        //开始循环取出每个数字,再倒过来
        while(x != 0){
    
    
            //从右往左取数
            int ge = x % 10;
            //修改x的值
            x = x / 10;
            num = num *10 + ge;
        }
        System.out.println(num);
        //比较
        System.out.println(num == temp);
    }
}

求商和余数:
需求:给定两个数,被除数和除数(正数,且不超过int范围),将两个数相除,要求不使用乘法、除法、和%
得到商和余数

public class test1 {
    
    
    public static void main(String[] args) {
    
    
        //被除数 / 除数 = 商...余数
        //定义被除数和除数
        int dividend = 100;
        int divisor = 3;
        //减的次数就是商
        int count = 0;
        while(dividend >= divisor){
    
    
           dividend = dividend - divisor;
           count++;
        }
        System.out.println("余数:" + dividend);
        System.out.println("商:" + count);
    }
}

do…while循环

在这里插入图片描述
先执行后判断

猜你喜欢

转载自blog.csdn.net/qq_64451048/article/details/127607802
今日推荐