java 第八章_零基础学Java第八章

一、循环流程控制语句

1.1for循环的格式及基本使用

1.1.1 for循环语句格式:

for(初始化语句;判断条件语句;控制条件语句) {

循环体语句;

}

1.1.2 执行流程

1):执行初始化语句

2):执行判断条件语句,看其结果是true还是false

如果是false,循环结束。

如果是true,继续执行。

3):执行循环体语句

4):执行控制条件语句

5):回到B继续

1.1.3 执行流程for循环的执行流程图

41d0abff88033c8e9eb220f25d5e4622.png

1.1.4 代码案例

package com.test.day03;

/*

* for循环语句格式:

* for(初始化语句;判断条件语句;控制条件语句) {

* 循环体语句;

* }

*

* 执行流程:

* A:执行初始化语句

* B:执行判断条件语句,看结果是true还是false

* 如果是true,就继续执行

* 如果是false,就结束循环

* C:执行循环体语句

* D:执行控制条件语句

* E:回到B继续

*

* 需求:

* 在控制台输出10次”HelloWorld”的案例。

*/

public class ForDemo {

public static void main(String[] args) {

// 原始写法

System.out.println("HelloWorld");

System.out.println("HelloWorld");

System.out.println("HelloWorld");

System.out.println("HelloWorld");

System.out.println("HelloWorld");

System.out.println("HelloWorld");

System.out.println("HelloWorld");

System.out.println("HelloWorld");

System.out.println("HelloWorld");

System.out.println("HelloWorld");

System.out.println("-------------------------");

// 用循环改进

for (int x = 1; x <= 10; x++) {

System.out.println("HelloWorld");

}

}

}

1.2for循环的练习

1.2.1 for循环实现获取指定范围数据

示例代码。仅供参考

package com.test.day03;

/*

* 需求:获取数据1-5和5-1

*/

public class ForDemo {

public static void main(String[] args) {

// 原始做法

System.out.println(1);

System.out.println(2);

System.out.println(3);

System.out.println(4);

System.out.println(5);

System.out.println("-------------");

// 用循环改进

for (int x = 1; x <= 5; x++) {

System.out.println(x);

}

System.out.println("-------------");

// 1-5的数据我们获取到了,如何获取5-1呢?

for (int x = 5; x >= 1; x--) {

System.out.println(x);

}

}

}

1.2.2 for循环实现1-5之间数据求和

示例代码。仅供参考

package com.test.day03;

/*

* 需求:求出1-5之间数据之和

*

* 分析:

* A:定义求和变量,初始化值是0

* B:获取1-5之间的数据,用for循环实现

* C:把每一次获取到的数据,累加起来就可以了

* D:输出求和变量即可

*/

public class ForDemo {

public static void main(String[] args) {

// 定义求和变量,初始化值是0

int sum = 0;

// 获取1-5之间的数据,用for循环实现

for (int x = 1; x <= 5; x++) {

// 把每一次获取到的数据,累加起来就可以了

// sum = sum + x;

/*

* 第一次:sum = 0 + 1 = 1 第二次:sum = 1 + 2 = 3 第三次:sum = 3 + 3 = 6 第四次:sum = 6 + 4 =

* 10 第五次:sum = 10 + 5 = 15

*/

sum += x;

}

// 输出求和结果

System.out.println("sum:" + sum);

}

}

1.2.3 for循环实现1-100之间偶数和

示例代码。仅供参考

package com.test.day03;

/*

* 需求:求出1-100之间偶数和

*

* 分析:

* A:定义求和变量,初始化值是0

* B:获取1-100之间的数据,用for循环实现

* C:把获取到的数据进行判断,看是否是偶数

* 如果是,就累加

* D:输出求和结果

*/

public class ForDemo {

public static void main(String[] args) {

// 定义求和变量,初始化值是0

int sum = 0;

// 获取1-100之间的数据,用for循环实现

for (int x = 1; x <= 100; x++) {

// 把获取到的数据进行判断,看是否是偶数

if (x % 2 == 0) {

sum += x;

}

}

// 输出求和结果

System.out.println("sum:" + sum);

}

}

1.2.4 for循环实现在控制台打印水仙花数

示例代码。仅供参考

package com.test.day03;

/*

* 需求:在控制台输出所有的”水仙花数”

*

* 分析:

* 什么是水仙花数呢?

* 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。

*举例:153就是一个水仙花数。

*153 = 1*1*1 + 5*5*5 + 3*3*3

*

*A:三位数其实就告诉了我们水仙花数的范围

*100-999

*B:如何获取一个数据的每一个位上的数呢?

*举例:我有一个数据153,请问如何获取到个位,十位,百位

*个位:153%10 = 3;

*十位:153/10%10 = 5;

*百位:153/10/10%10 = 1;

*千位:...

*万位:...

*C:让每个位上的立方和相加,并和该数据进行比较,如果相等,就说明该数据是水仙花数,在控制台输出

*/

public class ForDemo {

public static void main(String[] args) {

// 通过循环获取到每一个三位数

for (int x = 100; x < 1000; x++) {

// 获取个位,十位,百位

int ge = x % 10;

int shi = x / 10 % 10;

int bai = x / 10 / 10 % 10;

// 让每个位上的立方和相加,并和该数据进行比较,如果相等,就说明该数据是水仙花数,在控制台输出

if ((ge * ge * ge + shi * shi * shi + bai * bai * bai) == x) {

System.out.println(x);

}

}

}

}

1.2.5 for循环实现统计水仙花的个数

示例代码。仅供参考

package com.test.day03;

/*

* 需求:统计”水仙花数”共有多少个

*

* 分析:

* A:定义统计变量,初始化值是0

* B:获取三位数,用for循环实现

* C:获取三位数的个位,十位,百位

* D:判断这个三位数是否是水仙花数,如果是,统计变量++

* E:输出统计结果就可以了

*/

public class ForDemo {

public static void main(String[] args) {

// 定义统计变量,初始化值是0

int count = 0;

// 获取三位数,用for循环实现

for (int x = 100; x < 1000; x++) {

// 获取三位数的个位,十位,百位

int ge = x % 10;

int shi = x / 10 % 10;

int bai = x / 10 / 10 % 10;

// 判断这个三位数是否是水仙花数,如果是,统计变量++

if ((ge * ge * ge + shi * shi * shi + bai * bai * bai) == x) {

count++;

}

}

// 输出统计结果就可以了

System.out.println("水仙花数共有:" + count + "个");

}

}

1.3while循环的格式及基本使用

1.3.1 while循环语句格式

基本格式

while(判断条件语句) {

循环体语句;

}

扩展格式

初始化语句;

while(判断条件语句) {

循环体语句;

控制条件语句;

}

1.3.2 执行流程图

ee77633b1a53199762f8de3bace42a65.png

代码案例

package com.test.day03;

/*

* while循环语句的基本格式:

* while(判断条件语句) {

* 循环体语句;

* }

* 扩展格式:

* 初始化语句;

* while(判断条件语句) {

* 循环体语句;

* 控制条件语句;

* }

*

* 回顾for循环的语句格式:

* for(初始化语句;判断条件语句;控制条件语句) {

* 循环体语句;

* }

*/

public class WhileDemo {

public static void main(String[] args) {

// 输出10次HelloWorld

/*

* for(int x=1; x<=10; x++) { System.out.println("HellloWorld"); }

*/

// while循环实现

int x = 1;

while (x <= 10) {

System.out.println("HellloWorld");

x++;

}

}

}

2.4while循环的练习

1)while循环实现1-100之间数据求和

示例代码,仅供参考

package com.test.day03;

/*

* 求1-100之和。

* 练习:统计水仙花个数。

*/

public class WhileTest {

public static void main(String[] args) {

// 回顾for循环实现

/*

* //定义求和变量 int sum = 0; //获取1-100之间的数据 for(int x=1; x<=100; x++) { //累加 sum +=

* x; } System.out.println("1-100的和是:"+sum);

*/

// while循环实现

// 定义求和变量

int sum = 0;

int x = 1;

while (x <= 100) {

sum += x;

x++;

}

System.out.println("1-100的和是:" + sum);

}

}

1.5do…while循环的格式及基本使用

1.5.1 do…while循环语句格式

基本格式

do {

循环体语句;

}while((判断条件语句);

扩展格式

初始化语句;

do {

循环体语句;

控制条件语句;

} while((判断条件语句);

1.5.2 执行流程图

ef0724c8873d64924f611cf9f24f94d8.png

1.5.3 代码案例

package com.test.day03;

/*

* do...while循环的基本格式:

* do {

* 循环体语句;

* }while(判断条件语句);

* 扩展格式:

* 初始化语句;

* do {

* 循环体语句;

* 控制条件语句;

* }while(判断条件语句);

* 执行流程:

* A:执行初始化语句;

* B:执行循环体语句;

* C:执行控制条件语句;

* D:执行判断条件语句,看是true还是false

* 如果是true,回到B继续

* 如果是false,就结束

*

* 练习:

* 求和案例

* 统计水仙花个数

*/

public class DoWhileDemo {

public static void main(String[] args) {

// 输出10次 HelloWorld

/*

* for(int x=1; x<=10; x++) { System.out.println("HelloWorld"); }

*/

// do...while改写

int x = 1;

do {

System.out.println("HelloWorld");

x++;

} while (x <= 10);

}

}

1.6三种循环的区别

1.6.1 区别概述

虽然可以完成同样的功能,但是还是有小区别:

1)do…while循环至少会执行一次循环体。

2)for循环和while循环只有在条件成立的时候才会去执行循环体

3)for循环语句和while循环语句的小区别:

使用区别:控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,而while循环结束还可以继续使用,如果你想继续使用,就用while,否则推荐使用for。原因是for循环结束,该变量就从内存中消失,能够提高内存的使用效率。

代码案例

package com.test.day03;

/*

* 三种循环的区别:

* A:do...while至少执行一次循环体

* B:for,while循环先判断条件是否成立,然后决定是否执行循环体

*

* for和while的小区别:

* for循环的初始化变量,在循环结束后,不可以被访问。而while循环的初始化变量,是可以被继续使用的。

* 如果初始化变量,后面还要继续访问,就使用while,否则,推荐使用for。

*

* 循环的使用推荐:

* for -- while -- do...while

*/

public class DoWhileDemo {

public static void main(String[] args) {

/*

* int x = 3; while(x<3) { System.out.println("我爱林青霞"); x++; }

* System.out.println("--------------"); int y = 3; do {

* System.out.println("我爱林青霞"); y++; }while(y<3);

*/

for (int x = 1; x <= 10; x++) {

System.out.println("爱生活,爱Java");

}

// 这里的x无法继续访问

// System.out.println(x);

System.out.println("-----------------");

int y = 1;

while (y <= 10) {

System.out.println("爱生活,爱Java");

y++;

}

System.out.println(y);

}

}

关注公众号'巧叹',获取更多知识点和分布式系统项目源码及视频,300多个视频等你来拿

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值