java画各种五角星
程序员文章站
2022-06-07 16:30:34
...
Java 根据输入的值动态的画出各种五角星同时控制其大小(利用星号),主要实体的,空心的,平移的,利用for循环对初学者有一定的帮助。可以通过输入n(n>3)动态的控制五角星的大小,下面是完整代码和显示结果:
public class WJXUtils { /** * @param args */ public static void main(String[] args) { //实体五角星 // createWJXS(4); // 实体五角星平移 // createWJXSY(4,2,1); //空心五角星移动(星星外面也是空心) // createWJXKY(4,6,6); //空心五角星 // createWJXK(6); //空心外部区域全部显示星星的空心 //createWJXKS(8); //空心外部区域全部显示星星的空心的五角星平移 createWJXKSY(4,6,7); } //K表示区域总共4个,i表示行,j表示列 空心 public static void createWJXK(int n){ int i,i21,i22,i3,i4,j,j2,j21,j22,j3,j4,k=0; int m2=0; int m3=0; for( k=1;k<=4;k++){ //part 1 if(k==1){ for( i=0;i<n;i++){ //最大列数为6*n for( j=0;j<6*n;j++){ if((j<3*n-i+1&&j>=3*n-i )||(j<=3*n+i&&j>3*n+i-1) ){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } } if(k==2){ //part 2 for(j2=1;j2<=6*n+1;j2++){ if((j2<=2*n+1)||(j2>4*n&&j2<=6*n)){ System.out.print("*"); }else{ System.out.print(" "); } } for( i21=n+1;i21<n+2;i21++){ System.out.println(); for( j21=0;j21<6*n;j21++){ if((j21>=3&&j21<4)||(j21>6*n-4&&j21<=6*n-3)){ System.out.print("*"); }else{ System.out.print(" "); } } } for( i22=n+2;i22<n+3;i22++){ System.out.println(); for( j22=0;j22<=6*n;j22++){ if((j22>=5&&j22<6)||( j22>6*n-6&&j22<=6*n-5)){ System.out.print("*"); }else{ System.out.print(" "); } } } } if(k==3){ //part 3 for( i3=0;i3<n-3;i3++){ System.out.println(); for(j3=0;j3<6*n;j3++){ if((j3>5+m2&&j3<=5+m2+1)||((j3>=6*n-(5+m2)-1)&&j3<(6*n-(5+m2)))){ System.out.print("*"); }else{ //显示该部分中间那个点 if(i3==n-4&&j3==3*n){ System.out.print("*"); }else{ System.out.print(" "); } } } m2++; } } if(k==4){ //part4 for( i4=0;i4<n-1;i4++){ System.out.println(); for( j4=0;j4<6*n;j4++){ if((j4>=n+1-m3&&j4<n+2-m3)||(j4>=3*n-3-3*m3&&j4<3*n-2-3*m3)||((j4>3*n+2+3*m3)&&j4<=3*n+2+3*m3+1)||(j4>5*n+m3-2&&(j4<=5*n+m3-1))){ System.out.print("*"); }else{ System.out.print(" "); } } m3++; } } } } //K表示区域总共4个,i表示行,j表示列 里面空心外面全部实心 public static void createWJXKS(int n){ int i,i21,i22,i3,i4,j,j2,j21,j22,j3,j4,k=0; int m2=0; int m3=0; for( k=1;k<=4;k++){ //part 1 no change if(k==1){ for( i=0;i<n;i++){ //最大列数为6*n for( j=0;j<6*n;j++){ if((j<3*n-i+1&&j>=0 )||(j<=6*n&&j>3*n+i-1) ){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } } if(k==2){ //part 2 for(j2=1;j2<=6*n+1;j2++){ if((j2<=2*n+1)||(j2>4*n&&j2<=6*n)){ System.out.print("*"); }else{ System.out.print(" "); } } for( i21=n+1;i21<n+2;i21++){ System.out.println(); for( j21=0;j21<6*n;j21++){ if((j21>=0&&j21<4)||(j21>6*n-4&&j21<=6*n)){ System.out.print("*"); }else{ System.out.print(" "); } } } for( i22=n+2;i22<n+3;i22++){ System.out.println(); for( j22=0;j22<=6*n;j22++){ if((j22>=0&&j22<6)||( j22>6*n-6&&j22<=6*n-1)){ System.out.print("*"); }else{ System.out.print(" "); } } } } if(k==3){ //part 3 for( i3=0;i3<n-3;i3++){ System.out.println(); for(j3=0;j3<6*n;j3++){ if((j3>=0&&j3<=5+m2+1)||((j3>=6*n-(5+m2)-1)&&j3<=6*n)){ System.out.print("*"); }else{ //显示该部分中间那个点 if(i3==n-4&&j3==3*n){ System.out.print("*"); }else{ System.out.print(" "); } } } m2++; } } if(k==4){ //part 4 for( i4=0;i4<n-1;i4++){ System.out.println(); for( j4=0;j4<6*n;j4++){ if((j4>=0&&j4<n+2-m3)||(j4>=3*n-3-3*m3&&j4<=3*n+2+3*m3+1)||(j4>5*n+m3-2&&(j4<=6*n))){ System.out.print("*"); }else{ System.out.print(" "); } } m3++; } } } } //K表示区域总共4个,i表示行,j表示列 实心 public static void createWJXS(int n){ int i,i21,i22,i3,i4,j,j21,j22,j3,j4,k=0; int m2=0; int m3=0; for( k=1;k<=4;k++){ //part 1 if(k==1){ for( i=0;i<n;i++){ System.out.println(); //最大列数为6*n for( j=0;j<6*n;j++){ if(j<=3*n+i&&j>=3*n-i){ System.out.print("*"); }else{ System.out.print(" "); } } } } if(k==2){ //part2 System.out.println(""); for(i=1;i<=6*n+1;i++){ System.out.print("*"); } for( i21=n+1;i21<n+2;i21++){ System.out.println(); for( j21=0;j21<6*n;j21++){ if(j21>=3&&j21<=6*n-3){ System.out.print("*"); }else{ System.out.print(" "); } } } for( i22=n+2;i22<n+3;i22++){ System.out.println(); for( j22=0;j22<=6*n;j22++){ if(j22>=5&&j22<=6*n-5){ System.out.print("*"); }else{ System.out.print(" "); } } } } if(k==3){ //part 3 for( i3=0;i3<n-3;i3++){ System.out.println(); for(j3=0;j3<6*n;j3++){ if(j3>5+m2&&j3<6*n-(5+m2)){ System.out.print("*"); }else{ System.out.print(" "); } } m2++; } } if(k==4){ //part 4 for( i4=0;i4<n-1;i4++){ System.out.println(); for( j4=0;j4<6*n;j4++){ if((j4>=n+1-m3&&j4<3*n-2-3*m3)||((j4>3*n+2+3*m3)&&(j4<=5*n+m3-1))){ System.out.print("*"); }else{ System.out.print(" "); } } m3++; } } } } //K表示区域总共4个,i表示行,j表示列 里面空心外面全部实心 五角星移动 public static void createWJXKSY(int n,int x_0,int y_0){ int i,i21,i22,i3,i4,j,j2,j21,j22,j3,j4,k=0; int m2=0; int m3=0; int x,y=0; //向下移动 for( x=0;x<x_0;x++ ){ for(y=0;y<6*n+2*y_0;y++){ System.out.print("*"); } System.out.println(); } for( k=1;k<=4;k++){ //part 1 no change if(k==1){ for( i=0;i<n;i++){ //最大列数为6*n for( j=0;j<6*n+2*y_0;j++){ if((j<3*n-i+1+y_0&&j>=0 )||(j<=6*n+2*y_0&&j>3*n+i-1+y_0) ){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } } if(k==2){ //part 2 for(j2=1;j2<=6*n+1+2*y_0;j2++){ if((j2<=2*n+1+y_0)||(j2>4*n+y_0&&j2<=6*n+2*y_0)){ System.out.print("*"); }else{ System.out.print(" "); } } for( i21=n+1;i21<n+2;i21++){ System.out.println(); for( j21=0;j21<6*n+2*y_0;j21++){ if((j21>=0&&j21<4+y_0)||(j21>6*n-4+y_0&&j21<=6*n+2*y_0)){ System.out.print("*"); }else{ System.out.print(" "); } } } for( i22=n+2;i22<n+3;i22++){ System.out.println(); for( j22=0;j22<=6*n+2*y_0;j22++){ if((j22>=0&&j22<6+y_0)||( j22>6*n-6+y_0&&j22<=6*n-1+2*y_0)){ System.out.print("*"); }else{ System.out.print(" "); } } } } if(k==3){ //part 3 for( i3=0;i3<n-3;i3++){ System.out.println(); for(j3=0;j3<6*n+2*y_0;j3++){ if((j3>=0&&j3<=5+m2+1+y_0)||((j3>=6*n-(5+m2)-1+y_0)&&j3<=6*n+2*y_0)){ System.out.print("*"); }else{ //显示该部分中间那个点 if(i3==n-4&&j3==3*n+y_0){ System.out.print("*"); }else{ System.out.print(" "); } } } m2++; } } if(k==4){ //part 4 for( i4=0;i4<n-1;i4++){ System.out.println(); for( j4=0;j4<6*n+2*y_0;j4++){ if((j4>=0&&j4<n+2-m3+y_0)||(j4>=3*n-3-3*m3+y_0&&j4<=3*n+2+3*m3+1+y_0)||(j4>5*n+m3-2+y_0&&(j4<=6*n+2*y_0))){ System.out.print("*"); }else{ System.out.print(" "); } } m3++; } } } } //K表示区域总共4个,i表示行,j表示列 可以移动的五角星(实体) X表示距离上面大小,Y表示距离左面大小 public static void createWJXSY(int n,int x_0,int y_0){ int i,i21,i22,i3,i4,j,j21,j22,j3,j4,k=0; int x,y=0; int m2=0; int m3=0; //向下移动 for( x=0;x<x_0;x++ ){ System.out.println(); } for( k=1;k<=4;k++){ //part 1 if(k==1){ for(i=0;i<n;i++){ //最大列数为6*n for( j=0;j<6*n+y_0;j++){ if(j<=3*n+i+y_0&&j>=3*n-i+y_0){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } } if(k==2){ //part 2 for(i=1;i<=6*n+1+y_0;i++){ if(i>y_0){ System.out.print("*"); }else{ System.out.print(" "); } } for( i21=n+1;i21<n+2;i21++){ System.out.println(); for( j21=0;j21<6*n+y_0;j21++){ if(j21>=3+y_0&&j21<=6*n-3+y_0){ System.out.print("*"); }else{ System.out.print(" "); } } } for( i22=n+2;i22<n+3;i22++){ System.out.println(); for( j22=0;j22<=6*n+y_0;j22++){ if(j22>=5+y_0&&j22<=6*n-5+y_0){ System.out.print("*"); }else{ System.out.print(" "); } } } } if(k==3){ //part 3 for( i3=0;i3<n-3;i3++){ System.out.println(); for(j3=0;j3<6*n+y_0;j3++){ if(j3>5+m2+y_0&&j3<6*n-(5+m2)+y_0){ System.out.print("*"); }else{ System.out.print(" "); } } m2++; } } if(k==4){ //part 4 for( i4=0;i4<n-1;i4++){ System.out.println(); for( j4=0;j4<6*n+y_0;j4++){ if((j4>=n+1-m3+y_0&&j4<3*n-2-3*m3+y_0)||((j4>3*n+2+3*m3+y_0)&&(j4<=5*n+m3-1+y_0))){ System.out.print("*"); }else{ System.out.print(" "); } } m3++; } } } } //K表示区域总共4个,i表示行,j表示列 空心平移(外面也是空心) public static void createWJXKY(int n,int x_0,int y_0){ int i,i21,i22,i3,i4,j,j2,j21,j22,j3,j4,k=0; int m2=0; int m3=0; int x=0; //整个区域分正4个部分,也就是把原来的第2部分拆分成2个,这样更加好看 //向下移动 for( x=0;x<x_0;x++ ){ System.out.println(); } for( k=1;k<=4;k++){ //part 1 no change if(k==1){ for( i=0;i<n;i++){ //最大列数为6*n for( j=0;j<6*n+y_0;j++){ if((j<3*n-i+1+y_0&&j>=3*n-i+y_0 )||(j<=3*n+i+y_0&&j>3*n+i-1+y_0) ){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } } if(k==2){ //part 2 for(j2=1;j2<=6*n+1+y_0;j2++){ if((j2<=2*n+1+y_0&&j2>y_0)||(j2>4*n+y_0&&j2<=6*n+y_0)){ System.out.print("*"); }else{ System.out.print(" "); } } for( i21=n+1;i21<n+2;i21++){ System.out.println(); for( j21=0;j21<6*n+y_0;j21++){ if((j21>=3+y_0&&j21<4+y_0)||(j21>6*n-4+y_0&&j21<=6*n-3+y_0)){ System.out.print("*"); }else{ System.out.print(" "); } } } for( i22=n+2;i22<n+3;i22++){ System.out.println(); for( j22=0;j22<=6*n+y_0;j22++){ if((j22>=5+y_0&&j22<6+y_0)||( j22>6*n-6+y_0&&j22<=6*n-5+y_0)){ System.out.print("*"); }else{ System.out.print(" "); } } } } if(k==3){ //part 3 for( i3=0;i3<n-3;i3++){ System.out.println(); for(j3=0;j3<6*n+y_0;j3++){ if((j3>5+m2+y_0&&j3<=5+m2+1+y_0)||((j3>=6*n-(5+m2)-1+y_0)&&j3<(6*n-(5+m2)+y_0))){ System.out.print("*"); }else{ //显示该部分中间那个点 if(i3==n-4&&j3==3*n+y_0){ System.out.print("*"); }else{ System.out.print(" "); } } } m2++; } } if(k==4){ //part 4 for( i4=0;i4<n-1;i4++){ System.out.println(); for( j4=0;j4<6*n+y_0;j4++){ if((j4>=n+1-m3+y_0&&j4<n+2-m3+y_0)||(j4>=3*n-3-3*m3+y_0&&j4<3*n-2-3*m3+y_0)||((j4>3*n+2+3*m3+y_0)&&j4<=3*n+2+3*m3+1+y_0)||(j4>5*n+m3-2+y_0&&(j4<=5*n+m3-1+y_0))){ System.out.print("*"); }else{ System.out.print(" "); } } m3++; } } } } public static void crearWJXDemo(int n){ System.out.print("\n "); int i,j,k; //part 1 for(i=1;i<=n;i++) { for(j=1;j<=3*n-i+1;j++) System.out.print(" "); for(k=1;k<=2*i-1;k++) System.out.print("*"); System.out.println(" "); } //part 2 for(i=1;i<=6*n+1;i++) System.out.print("*"); System.out.print("\n "); for(i=1;i<=6*n-5;i++) System.out.print("*"); System.out.print("\n "); for(i=1;i<=6*n-9;i++) System.out.print("*"); System.out.print("\n"); //part 3 for(i=1;i<=n-3;i++){ for(j=1;j<=5+i;j++) System.out.print(" "); for(k=1;k<=6*n-9-2*i;k++) System.out.print("*"); System.out.print("\n"); } //part 4 for(i=1;i<=n-1;i++) { for(j=1;j<=n+2-i;j++) System.out.print(" "); for(k=1;k<=2*n-2*i-1;k++) System.out.print("*"); for(j=1;j<=6*i-1;j++)System.out.print(" "); for(k=1;k<=2*n-2*i-1;k++) System.out.print("*"); System.out.print("\n"); } } }各种显示结果如下: