004-循环结构
文章目录
while循环
语法
代码块00
初始化
while(条件){
循环体
迭代【控制条件发生变化】
}
代码块01
执行流程
int i = 1;
while(i <= 5){
System.out.println(i);
i++;
}
执行流程:
第一轮:
int i = 1; i<=5 ==> true; 执行输出; i++
第二轮:
i = 2; i<=5 ==> true; 执行输出; i++
第三轮:
i = 3; i<=5 ==> true; 执行输出; i++
第四轮:
i = 4; i<=5 ==> true; 执行输出; i++
第五轮:
i = 5; i<=5 ==> true; 执行输出; i++
第六轮
i = 6; i<=5 ==> false; 循环结束
输出练习
package com.shine.while0;
public class Demo03 {
public static void main(String[] args) {
/**
* 输出练习
* 1、输出1~100之间的整数
* 2、输出100~1之间的整数
*
* 3、输出1~100之间的偶数
* 4、输出1~100之间的奇数
*
* 5、输出a~z
* 6、输出Z~A
*/
// 1、输出1~100之间的整数
int i01 = 1;
while (i01 <= 10) {
System.out.println(i01);
i01++;
}
System.out.println("-------------");
// 2、输出100~1之间的整数
int i02 = 10;
while(i02 >= 1) {
System.out.println(i02);
i02--;
}
System.out.println("-------------");
int i03 = 1;
while (i03 <= 10) {
// 判断是否偶数
if (i03 % 2 == 0) {
System.out.println(i03);
}
// 无论如何都要递增
i03++;
}
System.out.println("-------------");
int i04 = 1;
while (i04 <= 10) {
// 判断是否奇数
if (i04 % 2 != 0) {
System.out.println(i04);
}
// 无论如何都要递增
i04++;
}
int i = 97;
while(i <= 122) {
System.out.println((char)i);
i++;
}
char c = 'a';
while (c <= 'z') {
System.out.println(c);
c++;
}
}
}
计算练习
package com.shine.while0;
public class Demo04 {
public static void main(String[] args) {
/**
* 计算练习
* 1、计算1~100累加的结果
* 2、计算1~100奇数/偶数累加的结果
* 3、计算1~10累乘的结果
*/
// 1、计算1~100累加的结果
// 初始化
int i01 = 1;
// 统计累加的结果
int result01 = 0;
while (i01 <= 10) {
// 累加
result01 += i01;
// 递增
i01++;
}
System.out.println(result01);
// 2、计算1~100奇数累加的结果
int i02 = 1;
int result02 = 0;
while (i02 <= 100) {
// 判断是否奇数
if (i02 %2 != 0) {
// 是奇数,累加
result02 += i02;
}
// 无论如何都要自增
i02++;
}
System.out.println(result02);
// 3、计算1~10累乘的结果
int i03 = 1; // 初始化,起始的数据
int result03 = 1; // 乘法中不能从0
while (i03 <= 10) {
// 累乘
result03 *= i03;
// 递增
i03++;
}
System.out.println(result03);
}
}
应用题
package com.shine.while0;
public class Demo05 {
public static void main(String[] args) {
/**
* 应用题
* 1、输出100~999之间的水仙花数字
*/
int num = 100;
while (num <= 999) {
// 获取每一位数字字面值
// 拆分三位数
/**
* 153
* 个位 153 % 10 = 3
* 十位 153 / 10 % 10 = 5
* 百位 153 / 100 = 1
*/
int g = num % 10;
int s = num / 10 % 10;
int b = num / 100;
// 计算立方和
int sum = g*g*g + s*s*s + b*b*b;
// 判断是否水仙花数字
if (sum == num) {
System.out.println("水仙花数字:" + num);
}
num++;
}
}
}
while循环的特殊情况
一次都不执行
- 循环第一轮判断,条件是false,循环直接结束
package com.shine.while0;
public class Demo01 {
public static void main(String[] args) {
System.out.println("开始..........");
int i = 0;
while (i>0 && i<=100) {
System.out.println(i);
i++;
}
System.out.println("结束..........");
}
}
根本停不下来
- 循环条件恒为true
package com.shine.while0;
public class Demo02 {
public static void main(String[] args) {
System.out.println("开始.............");
int i = 0;
while (i >= 0) { // 循环条件恒为true
System.out.println(i);
i++;
}
System.out.println("结束.............");
}
}
do-while【掌握】
概述
- 有些场景中循环必须先执行一次
- 比如输入密码、考试…
语法
初始化
do {
循环体
迭代
} while(条件);
- 最少执行一次
- 执行次数:1~N
package com.shine.while0;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
/**
* 模拟考试输入成绩,如果成绩达标,结束循环,否则继续考试。。。
*/
// 创建扫描器
Scanner sc = new Scanner(System.in);
int score;
do {
// 提示输入并获取考试成绩
System.out.println("请输入你的考试成绩:");
score = sc.nextInt();
} while(score < 60); // 如果成绩小于60分,视为不及格,继续输入成绩
System.out.println("OVER");
}
}
练习题
- 把while循环部分的输出/计算/应用题使用do-while实现一遍
for循环【掌握】
语法
for(初始化; 条件; 迭代){
循环体
}
执行流程
for(int i = 1; i <= 5; i++){
System.out.println(i);
}
执行流程:
第一轮:
int i = 1; i<=5 ==> true; 输出; i++
第二轮:
第三轮:
第四轮:
第五轮:
第六轮:
补全上述流程
案例
输出练习
package com.shine.for0;
public class Demo02 {
public static void main(String[] args) {
/**
* 输出练习
* 1、输出1~100之间的整数
* 2、输出1~100之间的奇数
* 3、输出a~z
*/
for (int i = 1; i <= 100; i++) {
System.out.print(i + ",");
}
System.out.println();
for (int i = 1; i <= 100; i+=2) {
System.out.print(i + ",");
}
System.out.println();
for (int i = 97; i < 123; i++) {
System.out.print((char)i + ",");
}
System.out.println();
for (char c = 'a'; c <= 'z'; c++) {
System.out.print(c + ",");
}
System.out.println();
}
}
计算练习
package com.shine.for0;
public class Demo03 {
public static void main(String[] args) {
/**
* 计算练习
* 1、计算1~100累加的结果
* 2、计算1~100奇数/偶数累加的结果
* 3、计算1~10累乘的结果
*/
int result = 0; // 累加的结果
for (int i = 1; i <= 100; i++) {
result += i; // 累加的操作
}
System.out.println(result);
int sum = 0; // 偶数求和
for (int i = 0; i <= 100; i+=2) { // +=2,每次增加2
sum += i; // 累加偶数
}
System.out.println(sum);
int mul = 1; // 累乘的结果,从1开始
for (int i = 1; i <= 10; i++) {
mul *= i;
}
System.out.println(mul);
}
}
应用题
package com.shine.for0;
public class Demo04 {
public static void main(String[] args) {
/**
* 1、输出100~999之间的水仙花数字
*/
for (int i = 100; i < 1000; i++) {
// 拆分三位数,得到每一个数字的字面值
int g = i % 10;
int s = i / 10 % 10;
int b = i / 100;
// 计算立方和
int sum = g*g*g + s*s*s + b*b*b;
// 判断
if (sum == i) {
System.out.println(i);
}
}
System.out.println("OVER");
}
}
for循环特殊情况
一次都不执行
根本停不下来
package com.shine.for0;
public class Demo05 {
public static void main(String[] args) {
for (int i = 0; i > 0; i++) { // 第一次判断为false,循环执行0次
System.out.println(i);
}
System.out.println("OVER");
for (int i = 0; i >= 0; i++) { // 循环条件恒为true
System.out.println(i);
}
System.out.println("OVER");
}
}
for循环中缺失部分内容
- 可以缺失一个或者多个循环组成部分
- 不能缺失;分号
package com.shine.for0;
public class Demo06 {
public static void main(String[] args) {
for (;;) {
System.out.println(777);
}
// System.out.println("OVER");
}
}
流程控制【掌握】
break
- 终止所在的循环,也可以终止指定的循环
package com.shine.process;
public class Demo01 {
public static void main(String[] args) {
System.out.println("开始。。。。。");
for (int i = 1; i < 10; i++) {
System.out.println(i);
if (i == 6) {
break; // 终止所在的循环,也可以终止指定的循环
}
}
System.out.println("结束。。。。。");
}
}
continue
- 跳过本轮循环后面的内容,直接进入下一轮循环
package com.shine.process;
public class Demo02 {
public static void main(String[] args) {
System.out.println("开始。。。。。");
for (int i = 1; i < 10; i++) {
if (i == 6) {
continue; // 跳过本轮循环后面的内容,直接进入下一轮循环
}
System.out.println(i);
}
System.out.println("结束。。。。。");
}
}
循环嵌套
概述
- 在循环的内部定义另一个完整的循环
打印图形
矩形
package com.shine.nest;
public class Demo01 {
public static void main(String[] args) {
/**
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
*/
outer:for (int i = 1; i <= 33; i++) {
inner:for (int j = 1; j <= 9; j++) {
System.out.print("* ");
}
System.out.println(i);
}
}
}
三角形
package com.shine.nest;
public class Demo02 {
public static void main(String[] args) {
/**
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
行数 i j j<=i
1 1 1
2 2 2
3 3 3
*/
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
/**
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
行数 i j i+j == 10 j <= 10-i
1 1 9
2 2 8
3 3 7
*/
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 10-i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
打印乘法表【掌握】【晨考】【周考】
package com.shine.nest;
public class Demo03 {
public static void main(String[] args) {
/**
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
行数 i j j<=i
1 1 1
2 2 2
3 3 3
*/
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i*j + "\t");
}
System.out.println();
}
}
}
应用题
package com.shine.nest;
public class Demo05 {
public static void main(String[] args) {
/**
* 今有鸡翁一,值钱伍;鸡母一,值钱三;鸡鶵三,值钱一。凡百钱买鸡百只,问鸡翁、母、鶵各几何?
答曰:鸡翁四,值钱二十;鸡母十八,值钱五十四;鸡鶵七十八,值钱二十六。
又答:鸡翁八,值钱四十;鸡 母十一,值钱三十三,鸡鶵八十一,值钱二十七。
又答:鸡翁十二,值钱六十;鸡母四、值钱十二;鸡鶵八十 四,值钱。”
*/
for (int g = 1; g < 20; g++) { // 公鸡范围:1~19
for (int m = 1; m < 33; m++) { // 母鸡的范围:1~33
for (int x = 3; x < 100; x+=3) { // 小鸡的范围:3~99,需要时3的倍数
// 判断
if ((g+m+x==100) && (g*5+m*3+x/3==100)) {
System.out.println("公鸡:" + g + ",母鸡:" + m + ",小鸡:" + x);
}
}
}
}
}
}
作业题
掌握今天笔记和代码中的内容
1、使用for和while输出九九乘法表
2、百鸡问题【挑战使用while实现】
3、循环录入5个学生的考试成绩,计算平均分
4、输出2~100之内的素数【困难】
5、模拟超市购物,从键盘录入整数, 选择购买的物品:
1可口可乐【3元】,2士力架【2.5元】,3脉动【5元】
4马可波罗【3元】,5大面包【5元】,6八宝粥【3元】
0==结束
如果是其他数, 继续循环
结束循环之后计算总价格
6、尝试完成笔记中的课堂作业ATM案例
7、预习函数的创建、调用、分类等内容
- 拓展题
- 9宫格写入1–9这些数字,要求每一行数字之和相等,每一列数字之和相等
问有几种组合方式 - 16宫格写入1–16这些数字,要求每一行数字之和相等,每一列数字之和相等
问有几种组合方式 - 编写方法,从键盘输入一个数n,判断是不是一个质数(质数是只能被1和它自身整除的数)
- 9宫格写入1–9这些数字,要求每一行数字之和相等,每一列数字之和相等