在黑马学习了半个月了,课程也上到了day07,在上到循环嵌套扩展题来说,对于一个0基础的小白我来说,纠结了好久没弄明白其中的原理,后来经过多次学习总结了一些东西:
public class zixi1 {
public static void main(String[] args) {
}
public static void lianXi1 () { //5x5的方块
for (int i = 1; i <= 5; i++) {
for (int j =0; j<=5; j++) {
System.out.print("1");
}
System.out.println();
}
}
public static void lianXi2() { //左上角直角三角形
for (int i = 1; i <= 5; i++) { //12345总共5行
for (int j = 1; j<= 6-i;j++) { //从1开始增循环每行的个数(54321)
System.out.print(1); //去ln不换行输出
}
System.out.println(); //连接到最外层循环
}
}
public static void lianXi3() { //左下角直角三角形
for (int i = 1; i <= 5; i++) { //固定格式定义5行 12345
for (int a = 1; a<=i; a++) { //第一行为一个第二行为2个以此类推到第五行
System.out.print(1); //固定去ln不换行输出
}
System.out.println(); //外层控制大换行连接到外层
}
}
public static void lianXi4() {
for (int i = 1; i <= 5; i++) { //12345
for (int b = 1; b < i; b++) { //2345
System.out.println(" ");
}
for (int a = 1; a <= 6 - i; a++) {
System.out.print(1);
}
System.out.println();
}
}
public static void lianXi5() { //乘法表
for (int i = 1; i <= 9; i++) { //123456789
for (int a = 1; a <= i; a++) {
System.out.print(a+"*"+i+"="+(i*a)+"\t");
}
System.out.println();
}
}
} |
|