Java学习笔记
循环结构
while循环
do…while循环
for循环
在java5中引入了一种主要用于数组的增强型for循环
while循环
语句:
while(布尔表达式){
//循环内容
}
- 只要布尔表达式为true,循环就会一直执行下去。
- 我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
- 少部分情况需要循环一直执行下去,比如服务器的请求响应监听等。
- 循环条件一直为true就会造成无限循环(死循环),我们正常的业务循环中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃。
- 思考:如何实现计算1+2+3+…+100=?
代码示例:
public class 循环结构 {
public static void main(String[] args) {
//计算1+2+3+...+100
int i =0;
int sum =0;
while(i<100){
i++;
sum=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 循环结构2 {
public static void main(String[] args) {
int a=0;
while(a<0){
System.out.println(a);
a++;
}
System.out.println("==========");
do {
System.out.println(a);
a++;
}while(a<0);
}
}
运行结果:
==========
0
For循环
- 虽然所有循环结构都可以用while或者do…while表示,但java提供了另一种语句-----for循环,使一些循环结构变得更加简单。
- for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
- for循环执行的次数是在执行前就确定的。
语法:
for(初始化;布尔表达式;更新){
// 代码语句
}
关于for循环的几点说明:
- 最先执行初始化步骤,可以声明一种类型,初始化一个或多个循环控制变量,也可以是空语句。
- 然后,检测布尔表达式的值,如果为true,循环体被执行,如果为false,循环终止,开始执行循环体后面的语句。
- 执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
- 再次检测布尔表达式,循环执行上面的过程。
练习1:计算0-100之间奇数和偶数的和
练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
练习3:打印九九乘法表
代码示例:
public class 循环结构3 {
public static void main(String[] args) {
//计算0-100之间奇数和偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0;i<=100;i++){
if (i%2!=0){
oddSum+=i;
}
else{
evenSum+=i;
}
}
System.out.println("奇数的和:"+oddSum);
System.out.println("偶数的和:"+evenSum);
}
}
运行结果:
奇数的和:2500
偶数的和:2550
代码示例:
public class 循环结构4 {
public static void main(String[] args) {
for (int i=1;i<=1000;i++){
//输出1-1000之间能被5整除的数,并且每行输出三个
if (i%5==0){
System.out.print(i+"\t");
//print 输出完不会换行 println 输出完换行
}
if (i%(5*3)==0){
System.out.println();//换行
//或者System.out.print("\n");
}
}
}
}
运行结果:
5 10 15
20 25 30
35 40 45
........
980 985 990
995 1000
代码示例:
public class 循环结构5 {
public static void main(String[] args) {
//打印九九乘法表
int x=0;
for(int i=1;i<10;i++){
for(int j=1;j<=i;j++){
//j<=i用来去除重复
System.out.print(i+"*"+j+"="+(i*j)+"\t");
// \t用于每行的间隔
}
System.out.println();//用于换行
}
}
}
运行结果:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
增强for循环
- 在数组中重点使用
- java5中引入了主要用于数组或集合的增强型for循环。
语法:
for(声明语句 : 表达式){
//代码句子
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者说返回值为数组的方法。
代码示例:
public class 循环结构6 {
public static void main(String[] args) {
int [] num = {10,20,30,40,50};//定义了一个数组
for (int i=0;i<5;i++){//数组中第一个元素下标是0
System.out.println(num[i]);//使用for循环遍历数组的元素
}
System.out.println("==============");
for (int x:num){
System.out.println(x);//使用for强循环遍历数组的元素
}
}
}
运行结果:
10
20
30
40
50
==============
10
20
30
40
50
Process finished with exit code 0
break continue
- break在任何循环的主体部分,均可使用break控制循环的流程,break用于强行退出循环,不执行循环着剩余的语句。(break语句也在switch语句中使用)。
- continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
代码示例:
public class 循环结构7 {
public static void main(String[] args) {
//打印101-150之间所有的质数
int cout = 0;
outer:for (int i =101;i<150;i++){// outer在这里是标签
for (int j =2;j<i;j++){
if(i%j==0){
continue outer;//直接用continue跳出循环,返回标签
}
}
System.out.println(i+" ");
}
}
}
运行结果:
101
103
107
109
113
127
131
137
139
149
打印三角形
代码示例:
public class 循环结构8 {
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();
}
}
}
运行结果:
*
***
*****
*******
*********