2021-09-07

流程控制06-12

9.2

循环结构

  • while循环
  • do…while循环
  • for循环
增强for循环
int[] numbers={10,20,30,40,50};
 for (int i = 0; i < 5; i++) {
     System.out.println(numbers[i]);
 }
 //遍历数组的元素
 for (int x:numbers){
     System.out.println(x);
 }

打印三角形

package struct;

public class TestDemo {
    //打印三角形 5行
    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();
        }
    }
}
上一篇:学习linux笔记(不断更新)


下一篇:栈的简单使用