双重for循环:
基本格式:
外围的循环执行一次,内围的循环执行一圈
一、打印平行四边形
public static void main(String[] args) {
// 打印平行四边形
for (int i = 1; i <=5 ; i++) {
for (int j = 1; j <=5-i ; j++) {
System.out.print(" ");
}
for (int k = 1; k <=5 ; k++) {
System.out.print("* ");
}
System.out.println();
}
}
二、打印三角形
public static void main(String[] args) {
for (int i = 1; i <=5 ; i++) {
for (int k = 1; k <=5-i ; k++) {
System.out.print(" ");
}
for (int j = 1; j <=2*i-1 ; j++) {
System.out.print("*");
}
System.out.println();
}
}
三、打印基础乘法表
public static void main(String[] args) {
for (int i = 1; i <=9 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
}
四、打印奇特乘法表(1)
代码如下:
public static void main(String[] args) {
for (int i = 1; i <=9 ; i++) {
for (int k = 1; k <i ; k++) {
System.out.print("\t\t");
}
for (int j = 10-i; j >=1 ; j--) {
System.out.print(j+"*"+(10-i)+"="+j*(10-i)+"\t");
}
System.out.println();
}
}
五、打印奇特乘法表(2)
代码如下:
public static void main(String[] args) {
for (int i = 1; i <=9 ; i++) {
for (int k = 1; k <i ; k++) {
System.out.print("\t");
}
for (int j = i; j <=9 ; j++) {
System.out.print(i+"*"+j+"="+i*j+"\t");
}
System.out.println();
}
}
六、打印奇特乘法表(3)
代码如下:
public static void main(String[] args) {
for (int i = 1; i <=9 ; i++) {
for (int j = 1; j <=(10-i) ; j++) {
System.out.print(i+"*"+j+"="+i*j+"\t");
}
System.out.println();
}
}