5.for循环语句

For循环

1.for循环练习1:奇数偶数和

知识小店:1.快捷生成for语句: 100.for + 回车

2.死循环: for(;;){};

package com.lin.study.foryuju;

public class ForDemo01 {
   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;
          }else{
               evenSum += i;
          }
      }
       System.out.println("奇数和oddSum=" + oddSum);
       System.out.println("偶数和evenSum=" + evenSum);
  }
}

运行结果:

奇数和oddSum=2500 偶数和evenSum=2550

Process finished with exit code 0

2.练习二:1~1000能被5整除的数

package com.lin.study.foryuju;

public class ForDemo02 {
   public static void main(String[] args) {
       //练习2: 分别使用for循环和while循环,
       // 输出1~1000之间能被5整除的数,并且每行输出3个数。

       //方式一:for循环
       for (int i = 1; i <= 1000; i++) {
           if(i%5==0){
               System.out.print(i + "\t");
          }
           if(i%(5*3)==0){
               System.out.println();//表示换行
               //System.out.println("\n");//也表示换行
          }
      }

       System.out.println();
       System.out.println("===============================");//分割线

       //方式二:while循环
       int i = 1;
       while(i<=1000){
           if(i%5==0){
               System.out.print(i + "\t");
          }
           if(i%(5*3)==0){
               System.out.println();
          }
           i++;
      }
  }
}

运行结果:

5 10 15 20 25 30

...

980 985 990

995 1000

=========================

5 10 15 20 25 30

...

995 1000 Process finished with exit code 0

3.九九乘法表

知识小店: 步骤

1.先打印第一列: System.out.println(1 + "&" + i + "=" + (1&i));

2.for循环嵌套 + 去重 : i<=j

3.调整样式: ...(i&j) + "\t");

package com.lin.study.foryuju;

public class ForDemo03 {
   public static void main(String[] args) {          //示例: 1*1=1
       
       //练习3:九九乘法表
       for (int j = 1; j <= 9; j++) {
           for (int i = 1; i <= j; i++) {
               System.out.print(i + "*" + j + "=" + (i*j) + "\t");
          }
           System.out.println();
      }
       
  }
}

运行结果://由于Typora的 * 有特殊含义,故而将 * 换成了 &

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

Process finished with exit code 0

4.增强for循环

package com.lin.study.foryuju;

public class ForDemo04 {
   public static void main(String[] args) {
       //增强for循环
       int[] numbers = {10,20,30,40,50};//定义了一个数组

       //方式一:普通for循环,遍历数组
       for (int i = 0; i < 5; i++) {
           System.out.println(numbers[i]);
      }
       
       System.out.println("。。。。。。。。。。。。。。。。。。。。。。");//分割线

       //方式二:增强for循环,遍历数组
       for (int x:numbers) {
           System.out.println(x);
      }

  }
}

运行结果:

10 20 30 40 50 。。。。。。。。。。。。。。。。。。。。。。 10 20 30 40 50

Process finished with exit code 0

5.关于goto语句,break,continue

  1. break: break语句用于强行退出循环。(辞职)

  2. continue: continue语句用于终止某次循环过程。(请假)

  3. goto: Java中没有goto语句,但可以通过带标签的break或continue体现

    示例:打印101~150之间所有的质数

    质数: 指的是在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。

package com.lin.study.foryuju;

public class LableDemo {
   public static void main(String[] args) {
       outer:for (int i = 100; i < 150; i++) {
           for (int j = 2; j <= i/2; j++) {//此处j <= i/2,表示去除重复判断。
               if(i%j==0){
                   continue outer;
              }
          }
           System.out.print(i + " ");
      }
  }
}

运行结果:

101 103 107 109 113 127 131 137 139 149 Process finished with exit code 0

6.小练习: 水仙花数

package com.lin.study.foryuju;

public class Test {
   public static void main(String[] args) {//打印水仙花数
       //水仙花数:一个三位正整数,且各数字的立方之和恰好等于该数。
       int i,j,k;
       for (int m = 100; m < 1000; m++) {
           i = m/100;
           j = m/10-i*10;
           k = m%10;
           if(m == i*i*i+j*j*j+k*k*k){
               System.out.print(m + " ");
          }
      }
  }
}

运行结果:

153 370 371 407 Process finished with exit code 0

7.打印三角阵

package com.lin.study.foryuju;

public class TestDemo {
   public static void main(String[] args) {
       //打印三角阵

       //方式一: 教材版
       int i,j,k;
       for (i = 1; i <= 5; i++) {
           for (j = 5; j > i; j--) {
               System.out.print(" ");
          }
           for (k = 1; k < 2*i; k++) {
               System.out.print("^");
          }
           System.out.println("");
      }
  }
}
package com.lin.study.foryuju;

public class TestDemo2 {
   public static void main(String[] args) {
       //打印三角阵
       //方式二: 狂神版
       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("");//换行
      }
  }
}

 

posted @ 2021-10-06 22:11  木木9_9  阅读(431)  评论(0)    收藏  举报