java入门day04:分支语句,循环语句

tips:从这里开始,思考的东西变多,需要    理解概念+分析题目的逻辑+多多练习

1.顺序结构

代码从上往下依次执行

2.分支结构

有两种: if 和 switch

2.1 if语句

2.1.1if语句的第一种格式

场景:只有结果成立才执行后面的代码

随堂案例:老丈人问女婿的流量如何,如果女婿的流量大于2,老丈人才搭理女婿

import java.util.Scanner;

public class demo1 {
    public static void main(String[] args) {
        System.out.println("请输入女婿的酒量");
        Scanner sc = new Scanner(System.in);
        if(a > 2) {
            System.out.println("小伙子,可以的!");
        }

    }
}

2.1.2 if第二种格式

场景:有两种情况要执行不同的代码时

 补充:如果执行了语句体1,就不会继续执行语句体2

随堂练习:商品金额为600。付款如果金额大于等于600,付款成功。如果金额小于600,付款失败

import java.util.Scanner;

public class demo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入付款金额");
        int money = sc.nextInt();
        if(money >= 600) {
            System.out.println("付款成功");
        } else {
            System.out.println("付款失败");

        }
    }
}

练习:电影院座位有100个,分为左右,左边为奇数坐,右边为偶数座。用户输入自己票号后提示用户是坐左边还是右边(坑:电影院座位只有100个,如果输入数字比100大。。。)

import java.util.Scanner;

public class demo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入票号");
        int ticket = sc.nextInt();

        if (ticket <=100 && ticket >= 0) {
            if (ticket % 2 != 0) {
                System.out.println("请坐左边");
            } else {
                System.out.println("请坐右边");

            }
        }
    }
}

2.1.3 if第三种格式

场景:用于3个及以上的情况时

练习:根据不同的分数送不同的礼物。

        如果是95~100分,送自行车一辆如

        果是90~94分,游乐场玩一天

        如果是80~89分,送变形金刚一个

        如果是80分,揍一顿。

tips:记得对0~100以外的数字进行异常处理

import java.util.Scanner;

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

        if(score >= 0 && score <= 100) {
            if(score <= 100 && score >= 95) {
                System.out.println("自行车一辆");
            }else if (score < 95 && score >= 90) {
                System.out.println("游乐场玩一圈");
            }else if (score < 89 && score >= 80) {
                System.out.println("送变形金刚一个");
            }else {
                System.out.println("揍一顿");
            }
        }else {
            System.out.println("当前数字不合法,请填写正确的成绩");
        }
    }
}

练习:
商场的VIP会员制,根据不同的会员会有不同的折假设商品总价为1000。
键盘录入会员级别,并计算出实际支付的钱。
会员1级:打9折。
会员2级:打8折。
会员3级:打7折。
非会员:打骨折。

import java.util.Scanner;

public class shopping {
    public static void main(String[] args) {
        int goods = 1000;
        Scanner sc = new Scanner(System.in);
        int level = sc.nextInt();

        if(level == 1){
            System.out.println("尊敬的1级会员,您需要支付"+ (goods * 0.9));
        }else if(level == 2){
            System.out.println("尊敬的2级会员,您需要支付"+ (goods * 0.8));
        }else if(level == 3){
            System.out.println("尊敬的3级会员,您需要支付"+ (goods * 0.7));
        }else {
            System.out.println("打骨折哦~亲");
        }
    }
}

2.2 switch语句

2.2.1switch基础用法

注意点:

 练习:键盘录入星期数,显示今天的减肥活动
周一:跑步
周二:游泳
周三:慢走
周四:动感单车
周五:拳击
周六: 爬山
周日:好好吃一顿

import java.util.Scanner;

public class sports {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int weekday = sc.nextInt();
        switch (weekday){
            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("不是一周中的任何一天");
        }
    }
}

2.2.2 switch其他情况

default的位置和省略 

位置 : defaule不一定是写在最下面的,我们可以写在任意位置。只不过习惯会写在最下面

省略 : defaule可以省略,语法不会有问题,但是不建议省略

case穿透:

就是因为case的后面没有找到break一直往下执行,就是case穿透(所以,一定要写break)

具体执行流程:

1.首先还是会拿着小括号中表达式的值跟下面每一个case进行匹配。
2.如果匹配上了,就会执行对应的语句体,如果此时发现了break,那么结束整个switch语句。
3.如果没有发现break,那么程序会继续执行下一个case的语句体,一直遇到break或者右大括号为止。

使用场景: 多个case的语句重复时,用case穿透简化代码。

和if语句的区别:

if 判断范围,在某范围内即可。

switch把有限的数据一一列举出来进行匹配,只能匹配一个,匹配不上执行default里的语句

 练习1:

键盘录入星期数,输出工作日和休息日(利用case穿透)

import java.util.Scanner;

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

        switch (num) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("工作日");
                break;
            case 6:
            case 7:
                System.out.println("休息日");
                break;
            default:
                System.out.println("没有这一天");
                break;

        }

    }
}

3.循环语句

3.1 for循环

执行顺序如下:1只执行一次,2-4不断循环,直到不满足判断条件,才会停止。

1.定义初始值。

2.判断i是不是小于等于10。

3.判断2为true时,执行这个语句。判断2为false,退出整个for循环。

4.i自增。

练习1:

倒着输出1~5;

public class for1 {
    public static void main(String[] args) {
        for(int i = 5;i>0;i--){
            System.out.println(i);
        }
    }
}
练习2:

玩游戏的时候,如果网不好那么会经常断线重连那么断线重连这个业务逐辑就需要重复执行。假设现在公司要求,断线重连的业务逻辑最多只写5次请用代码实现
备注:斯线重连的业务逐辑可以用输出语句替代。

public class for2 {
    public static void main(String[] args) {
        for(int i = 1;i<=5;i++) {
            System.out.println("第" + i + "次执行断线重连业务逻辑");
        }
    }
}
练习3:

需求:求1-5累加的和

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

注意点:当我们把累加和的变量定义在循环中时:

练习4:

求1-100之间,偶数的和

public class for3 {
    public static void main(String[] args) {
        int sum = 0;
        for(int i=1;i<=100;i++) {
           if(i%2==0) {
               sum+=i;
           }
        }
        System.out.println(sum);
    }
}

练习5:

键盘录入两个数字,表示一个范围。统计这个范围中,既能被3整除,又能被5整除数字有多少个?

import java.util.Scanner;

public class for4 {
    public static void main(String[] args) {
        System.out.println("请输入一个整数表示范围的开始");
        Scanner sc1 = new Scanner(System.in);
        int start = sc1.nextInt();
        System.out.println("请输入一个整数表示范围的结束");
        Scanner sc2 = new Scanner(System.in);
        int end = sc2.nextInt();
        int count = 0;
        for (int i = start; i <= end; i++) {
            if(i % 3 == 0 && i % 5 == 0) {
                System.out.println(i); // 用于查看满足条件的数字,验证count的数量是否正确
                count++;
            }
        }
        System.out.println(count);
    }
}

3.2 while循环

while循环的循环体和条件控制语句,都是写在大括号内,初始化语句在whitle外面,条件判断语句和for相同位置

执行顺序:

 练习1:

打印1-100之间的数字

public class while1 {
    public static void main(String[] args) {
        int i = 1; //初始语句 1 只执行一次
        while(i <= 100){ //判断语句 2 判断为true继续执行循环体
            System.out.println(i);  //循环体语句 3
            i++; //条件控制语句
        }
    }
}

for和while的区别

使用场景的区别

练习2:

世界最高山峰是珠穆朗玛峰(8844,43米=8844430毫米),假如我有一张足够大的纸,它的厚度是0.1毫米。请问,我折叠多少次,可以折成珠穆朗玛峰的高度?
分析: 折叠纸张: 每一次折叠纸张的厚度都是原先的两倍

double a = 0.1;

a=a*2;

a*= 2

public class while2 {
    public static void main(String[] args) {
        double height = 8844430; //珠穆朗玛峰高度 mm
        double paper = 0.1; //纸张的厚度 mm
        int count = 0; //折纸次数
        while(paper <= height){
            paper *= 2;
            System.out.println(paper); //本次循环折过后纸张的厚度
            count++;
        }
        System.out.println(count);
    }
}
练习3***:

给你一个整数 x 。如果 x 是一个回文整数,打印 true ,否则,返回 false 。

解释: 回文数是指正序《从左向右) 和倒序(从右向左) 读都是一样的整数。例如,121 是回文,而 123 不是

public class while3 {
    public static void main(String[] args) {
        int x = 121; //初始值
        int num = 0; //x倒过来之后的值
        int temp = x; // 临时变量,用于记录x的初始值(因为循环中x值会被修改)
        while(x != 0){ //判断条件是:x/10为0时,说明x的值没有数字了。
            int ge = x % 10; //拿到x的个位数
            System.out.println(ge);
            x = x / 10; //初始值x,去除掉x的个位数(后面循环去掉的依旧是x个位数)
            System.out.println(x);
            num = num * 10 + ge; //用num记录当前拿到的个位数,并且进行拼接,第一次循环拿到4,第二次循环拿到3 4 * 10 + 3 = 43,以此类推
            System.out.println(num);
        }
        System.out.println(num);
        System.out.println(num == temp); //对比倒过来的值和初始值,结果为true时,说明x的值是回文数
    }
}
练习4***:

给定两个整数,被除数和除数(都是正数,且不超过int的范围)将两数相除,要求不使用乘法、除法和 % 运算符。得到商和余数。
分析:
被除数  除数 = 商 ...余数(用减法,减法最后得出的数字为余数,减法执行次数就是商)

public class while4 {
    public static void main(String[] args) {
//        100-10 = 90
//        90-10 = 80.。。。
        int dividend = 1000; // 被除数
        int divisor = 19; //除数
        int count = 0;
        while(dividend >= divisor) {
            dividend = dividend - divisor;
            count++;
        }
        System.out.println("商为" + count);
        System.out.println("余数为" + dividend);
    }
}

3.3 do...while循环

如果第一次判断为false,也会执行一次(因为是先执行一次,再判断

day04的内容结束啦~

  • 23
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值