Java基础:循环结构
文章目录
- Java基础:循环结构
- while循环
- do...while循环
- for循环
- 练习1:计算0~100之间奇数和偶数的和
- 练习2:用while或for循环输出1~1000之间能被5整除的数,每行输出三个
- 练习3:打印九九乘法表
- 增强for循环
- break与contine
- 拓展
- 利用label打印101~150之间的所有质数(不建议使用)
- 打印三角形
- while循环
- do…while循环
- for循环
- 增强for循环(遍历数组)
while循环
- 语法
while (布尔表达式) {
布尔表达式为true将执行的语句
}
- 只要布尔表达式为true,循环就会一直执行下去
- 我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环
- 少部分情况需要循环一直执行,比如服务器的请求响应监听等
- 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死奔溃!
- 示例
public class WhileDemo01 {
public static void main(String[] args) {
//计算1~100的和
int i = 0;
int sum = 0;
while (i<100){//i从0到99
i++;
sum += i;
}
System.out.println(sum);
}
}
输出
5050
do…while循环
- 对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次
- do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次
- 语法
do {
执行语句
}while (布尔表达式);
- while和do…while的区别
- while先判断后执行。do…while是先执行后判断!
- do…while总是保证循环体会被至少执行一次!这是他们的主要差别
- 示例
public class DoWhileDemo01 {
public static void main(String[] args) {
//计算1~100
int i = 0;
int sum = 0;
do {
sum += i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
输出
5050
for循环
- 虽然所有循环结构都可以用 while 或者 do…while表示,但 Java 提供了另一种语句 —— for 循环,使一些循环结构变得更加简单
- for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构
- for循环执行的次数是在执行前就确定的
- 语法
for (初始化; 布尔表达式; 更新) {
执行语句
}
- 示例
- for和while对比
public class ForDemo01 {
public static void main(String[] args) {
int a = 1;//初始化条件
while (a <= 10){//条件判断
System.out.println(a);//循环体
a += 2;//迭代
}
System.out.println("While循环结束");
//初始化//判断//迭代
for(int i = 1;i <= 10;i++){
System.out.println(i);
}
System.out.println("For循环结束");
}
}
输出
1
3
5
7
9
While循环结束
1
2
3
4
5
6
7
8
9
10
For循环结束
练习1:计算0~100之间奇数和偶数的和
public class ForDemo02 {
public static void main(String[] args) {
//练习1:计算0~100之间奇数和偶数的和
int oddsum = 0;//奇数和
int evensum = 0;//偶数和
for (int i = 0; i <= 100; i++) {
if (i%2!=0) {
oddsum+=i;//oddsum = oddsum + i;
}else {
evensum += i;//evensum = evensum + i;
}
}
System.out.println("奇数的和:" + oddsum);
System.out.println("偶数的和:" + evensum);
}
}
输出
奇数的和:2500
偶数的和:2550
练习2:用while或for循环输出1~1000之间能被5整除的数,每行输出三个
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1~1000之间能被5整除的数,每行输出三个
for (int i = 1; i <= 1000; i++) {
if (i%5==0){
System.out.print(i + "\t");
}
if (i%15==0){
System.out.println();
}
}
}
}
输出
5 10 15
20 25 30
35 40 45
50 55 60
65 70 75
80 85 90
95 100 105
110 115 120
125 130 135
140 145 150
155 160 165
170 175 180
185 190 195
200 205 210
215 220 225
230 235 240
245 250 255
260 265 270
275 280 285
290 295 300
305 310 315
320 325 330
335 340 345
350 355 360
365 370 375
380 385 390
395 400 405
410 415 420
425 430 435
440 445 450
455 460 465
470 475 480
485 490 495
500 505 510
515 520 525
530 535 540
545 550 555
560 565 570
575 580 585
590 595 600
605 610 615
620 625 630
635 640 645
650 655 660
665 670 675
680 685 690
695 700 705
710 715 720
725 730 735
740 745 750
755 760 765
770 775 780
785 790 795
800 805 810
815 820 825
830 835 840
845 850 855
860 865 870
875 880 885
890 895 900
905 910 915
920 925 930
935 940 945
950 955 960
965 970 975
980 985 990
995 1000
练习3:打印九九乘法表
public class ForDemo04 {
public static void main(String[] args) {
//打印九九乘法表
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();
}
}
}
输出
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
增强for循环
- Java5 引入了一种主要用于数组的增强型 for 循环
- 语法
for( 声明语句 : 表达式)
{
执行语句
)
**声明语句:**声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
**表达式:**表达式是要访问的数组名,或者是返回值为数组的方法。
- 示例
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};//定义了一个数组
//遍历数组的元素
for (int x:numbers){
System.out.println(x);
}
System.out.println("===============");
//等价于上面那个for循环
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
}
}
输出
10
20
30
40
50
===============
10
20
30
40
50
break与contine
- break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
- 语法
break;
- 示例
public class BreakDemo01 {
public static void main(String[] args) {
int i = 0;
while (i < 100) {
i++;
System.out.println(i);
if (i==30){
break;//退出当前循环
}
}
}
}
输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
- 语法
continue;
- 示例
public class ContinueDemo01 {
public static void main(String args[]) {
int[] numbers = {10, 20, 30, 40, 50};
for (int x : numbers) {
if (x == 30) {
continue;//继续下个循环
}
System.out.println(x);
}
}
}
输出
10
20
40
50
拓展
- 关于goto关键字
- goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用。Java没有goto。然而,在break和continue这两个关键字的身上,我们仍然能看出一些goto的影子—带标签的break和continue。
- “标签”是指后面跟一个冒号的标识符,例如:label:
- 对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另-个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。
利用label打印101~150之间的所有质数(不建议使用)
public class LabelDemo {
public static void main(String[] args) {
//打印101~150之间的所有质数
//质数是只能被1和自身整除的数
int count = 0;
//outer是label的名字
outer:for (int i = 101; i < 150; i++) {
for (int j = 2; j < i/2; j++) {
if (i % j == 0){
continue outer;//返回到outer
}
}
System.out.print(i + "\t");
}
}
}
输出
101 103 107 109 113 127 131 137 139 149
打印三角形
public class TestDemo {
public static void main(String[] args) {
//打印三角形 5行
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
输出
*
***
*****
*******
*********