嵌套的方式有很多种,这里举例为for中嵌套for,在第一个的条件下,循环第二个for
public class Main { public static void main(String[] args) { for (int i = 0; i < 9; i++) { for (int j = 0; j < i + 1; j++) { System.out.print("*"); } System.out.println(); } } }
执行结果为:
* ** *** **** ***** ****** ******* ******** *********