05
3.循环结构
for循环
语法格式:
for(初始化语句
;判断条件语句
;控制条件语句
){
循环体语句
;
}
执行流程:
执行初始化语句
执行判断条件语句, 看其结果是true还是false
如果是false, 循环结束如果是true, 继续执行
执行循环体语句
执行条件控制语句
回到B继续执行
注意事项
大括号省略情况和if相同
public class Demo1 {
public static void main(String
[] args
) {
for (int i
= 1; i
< 10;i
++) {
for (int j
= 1; j
< i
+1; j
++) {
System
.out
.print(j
+"×"+i
+"="+(i
*j
)+"\t");
}
System
.out
.println();
}
}
}
public class Demo2 {
public static void main(String
[] args
) {
int num1
= 0;
int num2
= 0;
for (int i
= 1; i
< 101; i
++) {
if (i
% 2 == 1) {
num1
+= i
;
}
if (i
% 2 == 0) {
num2
+= i
;
}
}
System
.out
.println("奇数和为:" + num1
);
System
.out
.println("偶数和为:" + num2
);
}
}
public class Demo3 {
public static void main(String
[] args
) {
for (int i
= 1; i
< 101; i
++) {
if (i
%7==0) {
System
.out
.println("能被7整除的数为:"+i
);
}
if(i
%10==7|i
/10==7) {
System
.out
.println("包含7的整数为"+i
);
}
}
}
}
附:算法逻辑
1
121
12321
1234321
123454321
public class Test1 {
public static void main(String
[] args
) {
int i
,j
,k
,m
;
for(i
=1;i
<=5;i
++){
for(j
=1;j
<=5-i
;j
++){
System
.out
.print(" ");
}
for(k
=1;k
<=i
;k
++){
System
.out
.print(k
);
}
for(m
=i
-1;m
>0;m
--){
System
.out
.print(m
);
}
System
.out
.println();
}
}
}
import java
.util
.Scanner
;
public class Test2 {
public static void main(String
[] args
) {
Scanner scanner
= new Scanner(System
.in
);
System
.out
.println("输入n的值:");
int n
= scanner
.nextInt();
int sum
= 0;
int num
= 1;
for (int i
= 1; i
<= n
; i
++) {
num
*=i
;
sum
= sum
+ num
;
}
System
.out
.println("1!+2!+3!+…+n!="+sum
);
}
}
while循环
语法格式:
初始化语句;
while(判断条件语句)
{
循环语句体;
控制条件语句;
}
public class Demo4 {
public static void main(String
[] args
) {
int i
= 1;
while (i
<= 10) {
System
.out
.println(i
);
i
++;
}
i
= 10;
while (i
> 0) {
System
.out
.println(i
);
i
--;
}
i
= 1;
int sum
= 0;
while (i
<= 10) {
sum
+= i
;
i
++;
}
System
.out
.println("1-10的和为" + sum
);
int singlesum
= 0;
int doublesum
= 0;
i
= 1;
while (i
<= 100) {
if (i
% 2 == 1) {
singlesum
+= i
;
i
++;
}
if (i
% 2 == 0) {
doublesum
+= i
;
i
++;
}
}
System
.out
.println("奇数和为:" + singlesum
);
System
.out
.println("偶数和为:" + doublesum
);
}
}
附:
8
public class Test3 {
public static void main(String
[] args
) {
int i
= 100;
int a
,b
,c
;
while(i
<1000) {
a
= i
/100;
b
= i
/10%10;
c
= i
%10;
if (i
==a
*a
*a
+b
*b
*b
+c
*c
*c
) {
System
.out
.println(i
);
}
i
++;
}
}
}
do…while 循环
语法格式:
初始化语句;
do{
循环体语句;
控制体语句;
}while(判断条件语句);
public class Demo5 {
public static void main(String
[] args
) {
Scanner scanner
= new Scanner(System
.in
);
System
.out
.println("请输入密码:");
int pwd
= scanner
.nextInt();
do {
System
.out
.println("密码错误,请重新输入");
pwd
= scanner
.nextInt();
} while (pwd
!= 123456);
System
.out
.println("登录成功");
}
}
附:
public class Test4 {
public static void main(String
[] args
) {
double height
= 100.0;
double num
=height
/2;
double sum
=0;
int i
= 1;
do {
sum
+=num
;
num
/=2;
sum
=sum
+height
;
i
++;
System
.out
.println("第十次一共经过:" + sum
+ "米");
System
.out
.println("第十次反弹为:" + num
+ "米");
} while (i
== 10);
}
}
import java
.util
.Scanner
;
public class Test5 {
public static void main(String
[] args
) {
Scanner scanner
= new Scanner(System
.in
);
int socre
=0;
int num
=0;
System
.out
.println("请输入一个整数:");
do {
num
=scanner
.nextInt();
socre
+=10;
}
while(num
==1) ;
System
.out
.println("您的得分为:"+socre
);
}
}
4.跳出控制语句
break:结束单层循环
在循环语句中使用 ,离开使用场景没有意义 continue:结束本次循环
只能用在循环语句中,离开使用场景没有意义 return:结束整个方法
结束整个方法,并返回给方法的调用者main方法的调用者为 jvm
public class JumpCtrlTest {
public static void main(String
[] args
) {
for(int x
=1; x
<=10; x
++) {
if(x
%3==0) {
}
System
.out
.println("Java基础班");
}
}
}