java学习Day19

循环结构

whlie 循环

最基本的循环结构为:

while(){

//循环内容

}

概念

只要布尔表达式为ture,循环就会一直执行下去。

大多数情况是要循环停下来的,我们需要一个让表达式失效的方式来结束循环。

少部分需要循环一直执行,比如服务器的请求响应监听等。

循环一直为ture就会造成死循环,我们正常的业务编程中应尽量避免死循环。会造成程序性能或者造成程序卡死崩溃!

public class WhileDemo01 {
   public static void main(String[] args) {
       int i = 0;
       while (i<10){
           i++;
           System.out.println(i);
      }

  }
}

1
2
3
4
5
6
7
8
9
10

计算1+2+3+4+5......+99+100=?

public class WhileDemo02 {
  public static void main(String[] args) {
      //计算1+2+3....+100
      int i = 0;
      int sum = 0;
      while (i<=100){
          sum = sum + i;
          i++;
      }
      System.out.println(sum);
  }
}
5050

do...while循环

结构

do{

//代码语句

}while(布尔表达式);

概念

对于while语句而言,如果不满足条件,则不能进入循环

do...while循环和while循环相似,不同的是 do...while循环至少会执行一次。

public class DoWhileDemo01 {
   public static void main(String[] args) {
       int i = 0;
       int sum = 0;
       do {
           sum = sum + i;
           i++;
      }while (i<=100);
       System.out.println(sum);
  }
   5050

与while的区别

while先判断后执行。dowhile先执行后判断。

do..while总是保证循环地会至少执行一次,这是他们的主要区别。

public class DoWhileDemo02 {
   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循环

结构

for(初始化;布尔表达式;迭代){

//代码语句

}

public class ForDemo01 {
   public static void main(String[] args) {
       int a = 1;//初始化条件
       while (a<=100){//条件判断
           System.out.println(a);//循环体
           a+=2;//迭代
      }
       System.out.println("while循环结束");
           //初始化//条件判断//迭代
       for (int i =1;i<=100;i++){
           System.out.println(i);
      }
       System.out.println("for循环结束");
  }
}
1
3
5
...
95
97
99
while循环结束
1
2
3
4
5
6
7
...
98
99
100
for循环结束

概念

for循环使一些循环结构变得更加简单。

for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构

练习

计算0到100之间奇数和偶数的和

public class ForDemo02 {
   public static void main(String[] args) {
       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

用while或for循环输出1~1000之间能被5整除的数,并每行输出3个

public class ForDemo03 {
   public static void main(String[] args) {
       for (int i = 0; i <= 1000; i++) {
           if (i%5==0){
               System.out.print(i+"\t");
          }
           if (i%(3*5)==0){
               System.out.println();
               //sout("\n")
          }
      }
  }
}
0
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 970975
980985990
9951000

打印久久乘法表

第一步先打第一列


   public class ForDemo004 {
   public static void main(String[] args) {
       for (int i = 1; i <= 9; i++) {
           System.out.println(1+"*"+i+"="+(1*i));

      }
  }
}
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9

第二步吧固定的1用循环包起来,将1换成变量

public class ForDemo004 {
   public static void main(String[] args) {

       for (int j = 1; j <= 9; j++) {
           for (int i = 1; i <= 9; i++) {
               System.out.println(j+"*"+i+"="+(j*i));
          }
      }
  }
}
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
6*1=6
6*2=12
6*3=18
6*4=24
6*5=30
6*6=36
6*7=42
6*8=48
6*9=54
7*1=7
7*2=14
7*3=21
7*4=28
7*5=35
7*6=42
7*7=49
7*8=56
7*9=63
8*1=8
8*2=16
8*3=24
8*4=32
8*5=40
8*6=48
8*7=56
8*8=64
8*9=72
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

第三步去掉重复(使i<=j)

public class ForDemo004 {
   public static void main(String[] args) {

       for (int j = 1; j <= 9; j++) {
           for (int i = 1; i <= j; i++) {
               System.out.println(j+"*"+i+"="+(j*i));
          }
      }
  }
}
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

第四步调整样式

public class ForDemo004 {
   public static void main(String[] args) {

       for (int j = 1; j <= 9; j++) {
           for (int i = 1; i <= j; i++) {
               System.out.print(j+"*"+i+"="+(j*i)+"\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
posted @ 2022-09-05 14:30  新人用户  阅读(191)  评论(0)    收藏  举报