1 package cn.temptation;
2
3 public class Sample01 {
4 public static void main(String[] args) {
5 // for循环
6 // 格式
7 // for (int i = 初始值; i < value; i++) {
8 //
9 // }
10 // 口诀:左初值、右步长、条件在中间、处理在内部
11 // 1、左初值:声明一个变量并给变量赋值
12 // 2、右步长:控制左边声明的变量的变化幅度,为了让循环有跳出的可能
13 // 3、条件在中间:中间部分是循环的条件,当条件满足时,执行循环体中的语句;不满足时就不执行
14
15 // 需求:使用for循环输出1~5
16 for (int i = 1; i <= 5; i++) {
17 System.out.println(i);
18 }
19
20 // for循环执行顺序:(面试时可能被问到的问题)
21 // 可以结合调试模式查看变量的值的变化,得到如下结论:
22 // 1、第一次循环----->先做左初值的变量声明 和 赋初始化值
23 // 2、 ----->判断条件是否满足
24 // 3、 ----->进入到循环体中执行语句
25 // 4、 ----->执行完循环体中的语句后,再做右步长,控制左边变量的变化幅度
26 // 5、第二次循环----->从这次开始就不再做左初值,直接判断条件是否满足
27 // 6、 ----->进入到循环体中执行语句
28 // 7、 ----->执行完循环体中的语句后,再做右步长,控制左边变量的变化幅度
29 // 8、...
30 // 9、 ----->直到某次循环条件不再满足,跳出循环
31 }
32 }
1 package cn.temptation;
2
3 public class Sample02 {
4 public static void main(String[] args) {
5 // 需求:分别使用while循环 和 for循环输出 1~5
6
7 // while循环
8 int i = 1;
9 while (i <= 5) {
10 System.out.println(i);
11 i++;
12 }
13
14 // for循环
15 for (int j = 1; j <= 5; j++) {
16 System.out.println(j);
17 }
18
19 // 对比一下while循环 和 for循环
20 // 发现 while 循环 和 for 循环 差不多,只是把多行语句合并到一行里了
21 }
22 }
1 package cn.temptation;
2
3 public class Sample03 {
4 public static void main(String[] args) {
5 // 需求:使用for循环输出1~100
6 // 需求:使用for循环计算1+2+...+100
7
8 // 左初值:初始值由开发人员自己设置
9
10 // 写法1
11 for (int i = 1; i <= 100; i++) {
12 System.out.println(i);
13 }
14
15 // 写法2
16 for (int i = 0; i < 100; i++) {
17 System.out.println(i + 1);
18 }
19
20 // 写法3
21 for (int i = -1; i < 99; i++) {
22 System.out.println(i + 2);
23 }
24
25 // 注意:第11行、第16行、第21行都使用了变量i,之前讲变量名不能重复,这里为何没有提示语法错误?
26 // 因为变量的作用范围(作用域),这里的for循环中定义的变量,只能在该循环中可以使用,超出了该循环范围就无法使用了
27 // 语法错误:i cannot be resolved to a variable
28 // System.out.println(i);
29
30 // 定义总和的变量
31 int sum = 0;
32
33 for (int i = 1; i <= 100; i++) {
34 sum += i;
35 }
36
37 System.out.println("1+2+...+100=" + sum);
38 }
39 }
1 package cn.temptation;
2
3 public class Sample04 {
4 public static void main(String[] args) {
5 // 需求:使用for循环列出1~100之间的奇数
6
7 // 写法1、奇数的间隔为2
8 // for (int i = 1; i < 100; i += 2) {
9 // System.out.println(i);
10 // }
11 for (int i = 1; i < 100; i = i + 2) {
12 System.out.println(i);
13 }
14
15 // 写法2、奇数是和2求余为1的数
16 for (int i = 1; i <= 100; i++) {
17 if (i % 2 == 1) {
18 System.out.println(i);
19 }
20 }
21 }
22 }
1 package cn.temptation;
2
3 public class Sample05 {
4 public static void main(String[] args) {
5 // 需求:计算1~100之间的偶数的和
6 // int sum = 0;
7
8 // 写法1、偶数的间隔为2
9 // for (int i = 0; i <= 100; i += 2) {
10 // sum += i;
11 // }
12
13 // 写法2、偶数是和2求余数为0的数
14 // for (int i = 0; i <= 100; i++) {
15 // if (i % 2 == 0) {
16 // sum += i;
17 // }
18 // }
19
20 // System.out.println("1~100之间的偶数的和为:" + sum);
21
22 // 作为和的变量sum也在for循环中定义,如何写?
23 // 变量sum在循环结构的头部进行声明和赋初始化值,在循环体内可以使用它,但是在循环体外无法访问它
24 // for (int i = 0, sum = 0; i <= 100; i += 2) {
25 // sum += i;
26 // // 添加选择结构,让循环在最后一次输出结果
27 // if (i == 100) {
28 // System.out.println("1~100之间的偶数的和为:" + sum);
29 // }
30 // }
31
32 // System.out.println("1~100之间的偶数的和为:" + sum);
33
34 // 注意:如下写法,变量sum会在每次循环时重新设置为0,也就不会保存之前循环操作的结果
35 // 这也从侧面说明左初值中的内容只在第一次循环时执行,后续循环时都不会再执行
36 for (int i = 0; i <= 100; i++) {
37 int sum = 0;
38 sum += i;
39 System.out.println("1~100之间的偶数的和为:" + sum);
40 }
41 }
42 }
1 package cn.temptation;
2
3 public class Sample06 {
4 public static void main(String[] args) {
5 // 需求:使用for循环统计100~999之间的水仙花数并计算有多少个?
6
7 // 规则:以三位数字为例,数字的值 = (百位上的数字 * 百位上的数字 * 百位上的数字)
8 // + (十位上的数字 * 十位上的数字 * 十位上的数字)
9 // + (个位上的数字 * 个位上的数字 * 个位上的数字),则称为是一个水仙花数
10 // 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3
11
12 // 定义统计用个数变量
13 int count = 0;
14
15 for (int number = 100; number <= 999; number++) {
16 // 获取个位数字
17 int i = number / 1 % 10;
18 // 获取十位数字
19 int j = number / 10 % 10;
20 // 获取百位数字
21 int k = number / 100 % 10;
22
23 if (number == i * i * i + j * j * j + k * k * k) {
24 // 满足规则的数字输出
25 System.out.println(number);
26
27 count++;
28 }
29 }
30
31 System.out.println("100~999之间的水仙花数并计算有" + count + "个");
32 }
33 }
1 package cn.temptation;
2
3 public class Sample07 {
4 public static void main(String[] args) {
5 // 需求:使用for循环计算10的阶乘
6 // 阶乘:10! = 10 * 9 * 8 * 7 * ... * 1
7
8 // 定义结果变量
9 int result = 1;
10
11 // 写法1
12 // for (int i = 1; i <= 10; i++) {
13 // result *= i;
14 // }
15
16 // 写法2
17 for (int i = 10; i >= 1; i--) {
18 result *= i;
19 }
20
21 System.out.println("10的阶乘为:" + result);
22
23 // 注意:编程动手之前,先考虑一下程序执行结果的数值范围,选择合适的数据类型
24 }
25 }
1 package cn.temptation;
2
3 public class Sample08 {
4 public static void main(String[] args) {
5 // 需求:使用for循环解决百钱百鸡问题:母鸡一只5元,公鸡一只3元,小鸡三只1元,有100元,如何买到100只鸡?母鸡多少只?公鸡多少只?小鸡多少只?(母鸡、公鸡、小鸡都有)
6
7 // 思路:
8 // 从需求中得到一些等式
9 // 母鸡数量 + 公鸡数量 + 小鸡数量 = 100
10 // 母鸡单价 * 母鸡数量 + 公鸡单价 * 公鸡数量 + 小鸡单价 * 小鸡数量 = 100
11 // 小鸡数量是3的倍数
12
13 // 运用"穷举法"
14 // 如果买1只母鸡,再买1只公鸡,还能买多少只小鸡
15 // 如果买1只母鸡,再买2只公鸡,还能买多少只小鸡
16 // ...
17 // 如果买2只母鸡,再买1只公鸡,还能买多少只小鸡
18 // 如果买2只母鸡,再买2只公鸡,还能买多少只小鸡
19 // ...
20
21 System.out.println("使用for循环求解百钱百鸡问题:");
22
23 for (int i = 1; i < 20; i++) { // 计算母鸡
24 for (int j = 1; j < 33; j++) { // 计算公鸡
25 // 小鸡数量
26 int k = 100 - i - j;
27
28 if ((5 * i + 3 * j + k / 3 == 100) && (k % 3 == 0)) {
29 System.out.println("母鸡:" + i + "只,公鸡:" + j + "只,小鸡:" + k + "只");
30 }
31 }
32 }
33 }
34 }
1 package cn.temptation;
2
3 public class Sample09 {
4 public static void main(String[] args) {
5 // 需求:打印显示 4 * 4 正方形的点阵
6
7 // 写法1、最原始的写法
8 // System.out.println("****");
9 // System.out.println("****");
10 // System.out.println("****");
11 // System.out.println("****");
12
13 // 写法2、借助于 \r \n实现换行的操作
14 // System.out.println("****\r\n****\r\n****\r\n****");
15
16 // 思路:
17 // 1、在一行中把*号输出4次,形成****
18 // 2、再在3个不同的行上重复步骤1的操作
19
20 // 写法3、分步循环操作
21 // for (int i = 0; i < 4; i++) {
22 // System.out.print("*");
23 // }
24 // System.out.println();
25 // for (int i = 0; i < 4; i++) {
26 // System.out.print("*");
27 // }
28 // System.out.println();
29 // for (int i = 0; i < 4; i++) {
30 // System.out.print("*");
31 // }
32 // System.out.println();
33 // for (int i = 0; i < 4; i++) {
34 // System.out.print("*");
35 // }
36 // System.out.println();
37
38 // 写法4、针对写法3的改进,因为有发现相同的事情重复做,自然考虑使用循环
39 // 循环的嵌套
40 // 内部的操作时在一行中循环输出4个星号,把内部的操作作为一个整体,在外部再重复内部的这个操作3次
41
42 // 外部循环操作:重复内部的这个操作4次
43 for (int j = 0; j < 4; j++) {
44 // 内部循环操作:在一行中循环输出4个星号
45 for (int i = 0; i < 4; i++) {
46 System.out.print("*");
47 }
48 System.out.println();
49 }
50 }
51 }
1 package cn.temptation;
2
3 public class Sample10 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印如下图形
6 // *
7 // **
8 // ***
9 // ****
10 // *****
11
12 // 思路:找规律
13 // 1、第1行1个星号
14 // 2、第2行2个星号
15 // 3、第3行3个星号
16 // 4、第4行4个星号
17 // 5、第5行5个星号
18
19 // 分步操作
20 // for (int i = 0; i < 1; i++) {
21 // System.out.print("*");
22 // }
23 // // 换行
24 // System.out.println();
25 // for (int i = 0; i < 2; i++) {
26 // System.out.print("*");
27 // }
28 // // 换行
29 // System.out.println();
30 // for (int i = 0; i < 3; i++) {
31 // System.out.print("*");
32 // }
33 // // 换行
34 // System.out.println();
35 // for (int i = 0; i < 4; i++) {
36 // System.out.print("*");
37 // }
38 // // 换行
39 // System.out.println();
40 // for (int i = 0; i < 5; i++) {
41 // System.out.print("*");
42 // }
43 // // 换行
44 // System.out.println();
45
46 // 看到有重复的操作,考虑使用循环
47 // 看见有变化的量:1、2、3、4、5,考虑使用变量
48 // 通过找规律,我们发现这变量的值和其所在的行号一致
49 // 行号是什么?行号就是外层循环操作的当前次操作的数字,且这个数字是提供给内层循环来使用的数字(用于循环条件的控制)
50
51 // 写法1
52 // 外层循环
53 for (int i = 1; i <= 5; i++) {
54 // 内层循环
55 for (int j = 0; j < i; j++) {
56 System.out.print("*");
57 }
58 // 换行
59 System.out.println();
60 }
61
62 // 写法2
63 for (int i = 0; i < 5; i++) {
64 for (int j = 0; j <= i; j++) {
65 System.out.print("*");
66 }
67 // 换行
68 System.out.println();
69 }
70 }
71 }
1 package cn.temptation;
2
3 public class Sample11 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印如下图形
6 // *****
7 // ****
8 // ***
9 // **
10 // *
11
12 // 思路:找规律
13 // 1、第1行5个星号
14 // 2、第2行4个星号
15 // 3、第3行3个星号
16 // 4、第4行2个星号
17 // 5、第5行1个星号
18
19 // 写法1
20 for (int i = 0; i < 5; i++) {
21 for (int j = 4; j >= i; j--) {
22 System.out.print("*");
23 }
24 // 换行
25 System.out.println();
26 }
27
28 // 写法2
29 for (int i = 0; i < 5; i++) {
30 for (int j = 0; j < 5 - i; j++) {
31 System.out.print("*");
32 }
33 System.out.println();
34 }
35 }
36 }
1 package cn.temptation;
2
3 public class Sample12 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印如下图形
6 // *
7 // **
8 // ***
9 // ****
10 // *****
11
12 // 思路:
13 // 1、星号还是打印一样的星号
14 // 2、在每一行输出星号之前,先输出了空格
15 // 3、一个较复杂的问题分解为两个简单的问题来处理:分解为每行中先输出空格,再输出星号
16
17 for (int i = 0; i < 5; i++) {
18 // 输出空格的处理
19 for (int j = 0; j < 4 - i; j++) {
20 System.out.print(" ");
21 }
22
23 // 输出星号的处理
24 for (int j = 0; j <= i; j++) {
25 System.out.print("*");
26 }
27 System.out.println();
28 }
29 }
30 }
1 package cn.temptation;
2
3 public class Sample13 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印如下图形
6 // *****
7 // ****
8 // ***
9 // **
10 // *
11
12 for (int i = 0; i < 5; i++) {
13 // 打印空格
14 for (int j = 0; j < i; j++) {
15 System.out.print(" ");
16 }
17
18 // 打印星号
19 for (int j = 0; j < 5 - i; j++) {
20 System.out.print("*");
21 }
22
23 // 换行
24 System.out.println();
25 }
26 }
27 }
1 package cn.temptation;
2
3 public class Sample14 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印如下图形
6 // *
7 // ***
8 // *****
9 // *******
10 // *********
11
12 for (int i = 0; i < 5; i++) {
13 // 打印空格
14 for (int j = 0; j < 4 - i; j++) {
15 System.out.print(" ");
16 }
17
18 // 打印星号
19 for (int j = 1; j <= i * 2 + 1; j++) {
20 System.out.print("*");
21 }
22
23 // 换行
24 System.out.println();
25 }
26 }
27 }
1 package cn.temptation;
2
3 public class Sample15 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印如下图形
6 // *
7 // **
8 // ***
9 // ****
10 // *****
11 // ****
12 // ***
13 // **
14 // *
15
16 // 思路:不妨把这个问题分解为打印上部分 和 下部分
17
18 // 打印上部分
19 for (int i = 0; i < 5; i++) {
20 for (int j = 0; j <= i; j++) {
21 System.out.print("*");
22 }
23 // 换行
24 System.out.println();
25 }
26
27 // 打印下部分
28 for (int i = 0; i < 4; i++) {
29 for (int j = 0; j < 4 - i; j++) {
30 System.out.print("*");
31 }
32 // 换行
33 System.out.println();
34 }
35 }
36 }
1 package cn.temptation;
2
3 public class Sample16 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印如下图形
6 // *
7 // **
8 // ***
9 // ****
10 // *****
11 // ****
12 // ***
13 // **
14 // *
15
16 // 思路:不妨把这个问题分解为打印上部分 和 下部分
17
18 // 打印上部分
19 for (int i = 0; i < 5; i++) {
20 // 打印空格
21 for (int j = 0; j < 4 - i; j++) {
22 System.out.print(" ");
23 }
24 // 打印星号
25 for (int j = 0; j <= i; j++) {
26 System.out.print("*");
27 }
28 // 换行
29 System.out.println();
30 }
31
32 // 打印下部分
33 for (int i = 0; i < 4; i++) {
34 // 打印空格
35 for (int j = 0; j <= i; j++) {
36 System.out.print(" ");
37 }
38 // 打印星号
39 for (int j = 0; j < 4 - i; j++) {
40 System.out.print("*");
41 }
42 // 换行
43 System.out.println();
44 }
45 }
46 }
1 package cn.temptation;
2
3 public class Sample17 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印如下图形
6 // *
7 // ***
8 // *****
9 // *******
10 // *********
11 // *******
12 // *****
13 // ***
14 // *
15
16 // 思路:不妨把这个问题分解为打印上部分 和 下部分
17
18 // 打印上部分
19 for (int i = 0; i < 5; i++) {
20 // 打印空格
21 for (int j = 0; j < 4 - i; j++) {
22 System.out.print(" ");
23 }
24
25 // 打印星号
26 for (int j = 1; j <= i * 2 + 1; j++) {
27 System.out.print("*");
28 }
29
30 // 换行
31 System.out.println();
32 }
33
34 // 打印下部分
35 for (int i = 0; i < 4; i++) {
36 // 打印空格
37 for (int j = 0; j <= i; j++) {
38 System.out.print(" ");
39 }
40
41 // 打印星号
42 for (int j = 0; j <= 7 - i * 2 - 1; j++) {
43 System.out.print("*");
44 }
45
46 // 换行
47 System.out.println();
48 }
49 }
50 }
1 package cn.temptation;
2
3 public class Sample18 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印如下图形
6 // *
7 // * *
8 // * *
9 // * *
10 // * *
11 // * *
12 // * *
13 // * *
14 // *
15
16 // 思路:不妨把这个问题分解为打印上部分 和 下部分
17
18 // 打印上部分
19 for (int i = 0; i < 5; i++) {
20 // 打印空格
21 for (int j = 0; j < 4 - i; j++) {
22 System.out.print(" ");
23 }
24
25 // 打印星号
26 System.out.print("*");
27
28 if (i != 0) { // 第一行不执行
29 // 打印空格
30 for (int j = 0; j < 2 * i - 1; j++) {
31 System.out.print(" ");
32 }
33
34 // 打印星号
35 System.out.print("*");
36 }
37
38
39 // 换行
40 System.out.println();
41 }
42
43 // 打印下部分
44 for (int i = 0; i < 4; i++) {
45 // 打印空格
46 for (int j = 0; j <= i; j++) {
47 System.out.print(" ");
48 }
49
50 // 打印星号
51 System.out.print("*");
52
53 if (i != 3) { // 最后一行不执行
54 // 打印空格
55 for (int j = 0; j < 5 - 2 * i; j++) {
56 System.out.print(" ");
57 }
58
59 // 打印星号
60 System.out.print("*");
61 }
62
63 // 换行
64 System.out.println();
65 }
66 }
67 }
1 package cn.temptation;
2
3 public class Sample19 {
4 public static void main(String[] args) {
5 // 需求:使用for循环打印九九乘法口诀表
6
7 // 思路:
8 // 1、把没有遇见过的问题转换为遇见过的问题
9 // 2、只要把星号替换为算术表达式即可
10 // 3、使用"\t"实现制表符的效果
11
12 System.out.println("-----九九乘法口诀表-----");
13
14 for (int i = 1; i <= 9; i++) {
15 for (int j = 1; j <= i; j++) {
16 // System.out.print("*" + "\t");
17 System.out.print(j + "*" + i + "=" + (i * j) + "\t");
18 }
19 // 换行
20 System.out.println();
21 }
22 }
23 }
1 package cn.temptation;
2
3 public class Sample20 {
4 public static void main(String[] args) {
5 // break语句
6 // 1、switch语句中,结合case条件 或是 default语句,起的是跳出选择结构的作用
7 // 2、break用在循环中,起的跳出循环结构的作用,在某次循环执行过程中执行到break语句后跳出了循环,break之后的语句是执行不到的
8
9 // int i = 2;
10
11 // switch (i) {
12 // case 1:
13 // System.out.println("i的值为1");
14 // break;
15 // case 2:
16 // System.out.println("i的值为2");
17 // break;
18 // default:
19 // break;
20 // }
21
22 // while (i < 5) {
23 // System.out.println(i);
24 // i++;
25 // break;
26 // // 语法错误:Unreachable code
27 // System.out.println("这是break语句之后的语句");
28 // }
29
30 // Dead Code 描述右步长部分为死代码
31 // 再次从侧面证明了右步长是在循环体内的语句执行结束后去执行的
32 for (int j = 0; j < 3; j++) {
33 System.out.println(j);
34 break;
35 // 语法错误:Unreachable code
36 // System.out.println("这是break语句之后的语句");
37 }
38
39 // 注意:下句也是break语句后的语句,但是不受break语句的影响,因为break语句是for循环中的语句,是跳出循环的操作
40 System.out.println("如果看到这句,说明循环结束了");
41 }
42 }
1 package cn.temptation;
2
3 public class Sample21 {
4 public static void main(String[] args) {
5 // continue语句
6 // 1、continue语句不能用在循环以外的结构中
7 // 2、循环中的continue语句之后的语句会提示语法错误:无法到达的代码
8 // 3、continue语句表示跳出当前当次循环
9
10 int i = 2;
11
12 if (i == 3) {
13 System.out.println("i等于3");
14 // 语法错误:continue cannot be used outside of a loop
15 // continue;
16 }
17
18 // while (i < 5) {
19 // System.out.println(i);
20 // continue;
21 // // 语法错误:Unreachable code
22 //// System.out.println("continue语句之后的语句");
23 // }
24
25 // for (int j = 0; j < 5; j++) {
26 // if (j == 2) {
27 // continue;
28 // }
29 // System.out.println(j);
30 // }
31
32 // 对比break语句 和 continue语句的效果
33 // 结论:break语句跳出循环(后续循环条件满足也不执行了);continue语句跳出当前当次循环
34 for (int j = 0; j < 5; j++) {
35 if (j == 2) {
36 break;
37 }
38 System.out.println(j);
39 }
40 }
41 }
1 package cn.temptation;
2
3 public class Sample22 {
4 public static void main(String[] args) {
5 // 循环中死循环写法的总结
6
7 // while (true) {
8 // System.out.println("while死循环的写法");
9 // }
10
11 // do {
12 // System.out.println("do...while死循环的写法");
13 // } while (true);
14
15 // for (;;) {
16 // System.out.println("for死循环的写法");
17 // }
18
19 // 根据while 和 for 死循环的写法,稍作变形,侧面证明while结构 和 for结构是一样的
20 int i = 2;
21
22 // while (i < 5) {
23 // System.out.println(i);
24 // i++;
25 // }
26
27 for (; i < 5;) {
28 System.out.println(i);
29 i++;
30 }
31 }
32 }