for嵌套循环
简介:
public class Test06 {public static void main(String[] args){/*** 打印矩阵:* ****** ****** *****
public class Test06 {
public static void main(String[] args){
/**
* 打印矩阵:
* *****
* *****
* *****
*/
System.out.println("打印矩阵:");
for (int i = 0; i <3 ; i++) {
for (int j = 0; j <5; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("===========");
/**
* 打印正方形:
* * * *
* * * *
* * * *
* * * *
*/
System.out.println("打印正方形:");
for (int i = 0; i <4; i++) {
for (int j = 0; j <4; j++) {
System.out.print(" *");
}
System.out.println();
}
System.out.println("===========");
System.out.println("打印乘法口诀:");
for (int i = 1; i <=9; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
System.out.println("===========");
/*
打印左直角三角形:
*
**
***
****
*****
*/
System.out.println("打印左直角三角形:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j <=i; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("===========");
/**
打印倒左直角三角形:
*****
****
***
**
*
*/
System.out.println("打印倒左直角三角形:");
for (int i =1; i <=5; i++) {
for (int j =5; j >=i; j--) {
System.out.print("*");
}
System.out.println();
}
System.out.println("===========");
/*
打印右直角三角形:
*
**
***
****
*****
*/
System.out.println("打印右直角三角形:");
for (int i = 1; i <=5; i++) {
for (int j =5; j>0; j--) {
if (j<=i) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("===========");
/**
打印倒右直角三角形:
*****
****
***
**
*
*/
System.out.println("打印倒右直角三角形:");
for (int i =5; i>0; i--) {
for (int j=5; j>0; j--) {
if (j<=i) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("===========");
/**
打印等腰三角形:
*
***
*****
*******
*/
System.out.println("打印等腰三角形:");
for (int i = 1; i <5; i++) {
for (int x= 0; x<=5-i; x++) {
System.out.print(" ");
}
for (int j = 1; j <=i*2-1 ; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("===========");
/**
打印倒等腰三角形:
*****
***
*
*/
System.out.println("打印倒等腰三角形:");
for (int i = 1; i <=5; i++) {
for (int x= 4; x>5-i; x--) {
System.out.print(" ");
}
for (int j = 1; j <=(5-i)*2+1 ; j++) {//5:控制行数
System.out.print("*");
}
System.out.println();
}
System.out.println("===========");
/**
菱形
*
***
*****
***
*
*/
System.out.println("菱形");
for(int i=1;i<=3;i++){
for(int m=0;m<3-i;m++){
System.out.print(" ");
}
for(int j=1;j<=i*2-1;j++){
System.out.print("*");
}
System.out.println();
}
for(int i=1;i<=2;i++){
for(int m=1;m>=2-i;m--){
System.out.print(" ");
}
for(int j=1;j<=(2-i)*2+1;j++){
System.out.print("*");
}
System.out.println();
}
}
}