流程控制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();
}
}
}