Java笔记——流程控制LOOP循环 笔记二

Java笔记——流程控制LOOP循环 笔记二

本篇笔记2延续流程控制LOOP循环的讲解,如需要全部笔记,请 移步笔记1
loop 笔记一

ForTest3

public class ForTest3 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 1){
                //筛选奇数1 3 5 7 9
            sum += i;
            }
        }
        System.out.println("1-10中的奇数和为"+sum);

        System.out.println("-------------------");
        int sum2 = 0;
        for (int i = 1; i <= 10; i+=2) {
            sum2+=i;
        }
        System.out.println("1-10中的奇数和为"+sum);
    }
}

ForTest4

需求

	1·水仙花是一个三位数
    2·水仙花的个位,十位,百位的数字立方和等于原数
    分析:
    定义一个for循环,找100到999
    每次访问到数据后,提取该数据的个位,十位,百位数字
    使用if判断:个位,十位,百位的数字立方和是否邓宇原数,等于则输出该数

    个数:
    count
package com.mumei.loop;

public class ForTest4 {
    public static void main(String[] args) {
        
        int count = 0;
        for (int i = 100; i <= 999; i++) {
            int ge = i % 10;
            int shi = i /10 % 10;
            int bai = i / 100;
            //检索条件
            if (ge*ge*ge+shi*shi*shi+bai*bai*bai == i) {
                System.out.print(i+"\t");
                count++;
            }
        }
        System.out.println();//换行
        System.out.println("水仙花的个数是"+count);
    }
}

在这里插入图片描述

while

功能

while格式:
while (循环条件){
循环体语句;
迭代语句;
}
while (i ❤️){
System.out.println(“I donnt wanna wake up from this sweet sweet dream.–Enkidu”);
i++;
}
什么时候用for,什么时候用while
功能上完全一样,for能解决的while也能解决,反之亦然
使用范围:知道循环几次用for循环,不知道循环几次建议使用while

package com.mumei.loop;

public class WhileDemo5 {
    public static void main(String[] args) {
        //目标:学会使用while循环,并理解
        int i = 0;
        while (i <3){
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
            i++;
        }
        System.out.println("-----------------------");
        int j = 0;
        while (j < 3){
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
            j++;
        }
    }
}

在这里插入图片描述

WhileTest6

		需求:珠穆朗玛峰的高度是8848860mm,使纸张对折,问折叠多少次之后使纸的厚度达到珠穆朗玛峰的高度
    	纸张厚度0.1mm
package com.mumei.loop;

public class WhileTest6 {
    public static void main(String[] args) {

        //定义自变量
        double peakHeight = 8848860;
        double paperThickness = 0.1;

        //定义while循环
        int count = 0;//计数器
        while (paperThickness < peakHeight){
            paperThickness *=2;//纸张厚度增加
            count++;
        }
        System.out.println("折叠"+count+"次");
        System.out.println("纸张的最终厚度是"+paperThickness+"mm");
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

玄东林檎

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

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

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

打赏作者

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

抵扣说明:

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

余额充值