题目:
第一个图形function:
public static void numberOne(int lineNum,char ch){
//判断奇数偶数
if(lineNum%2==0){
lineNum+=1;//变成奇数
}
for(int i=1;i<=lineNum/2+1;i++){
for(int j=1;j<=lineNum;j++){
if(i==lineNum/2+1){
System.out.print(ch+"\t");
}else{
if(j==1||j==lineNum||j==i||j==lineNum-i+1){
System.out.print(ch+"\t");
}else{
System.out.print("\t");
}
}
}
System.out.println();
}
}
效果:
第二个图形function:
public static void numberTwo(int lineNum,char ch){
//判断奇数偶数
if(lineNum%2==0){
lineNum+=1;//变成奇数
}
for(int i=1;i<=lineNum;i++){
for(int j=1;j<=lineNum;j++){
if(i==1||i==lineNum){
System.out.print(ch+"\t");
}else{
if(i<lineNum/2+1){
if(j==1||j==lineNum){
System.out.print(ch+"\t");
}else{
System.out.print("\t");
}
}else{
if(j==1||j==lineNum||j==lineNum-i+1||j==i){
System.out.print(ch+"\t");
}else{
System.out.print("\t");
}
}
}
}
System.out.println();
}
}
效果:
第三个图形function:
public static void numberThree(int lineNum,char ch){
//判断奇数偶数
if(lineNum%2==0){
lineNum+=1;//变成奇数
}
for(int i=1;i<=lineNum;i++){
for(int j=1;j<=lineNum;j++){
if(i==1||i==lineNum){
System.out.print(ch+"\t");
}else{
if(i<=lineNum/2+1){
if(j==1||j==lineNum||j==i){
System.out.print(ch+"\t");
}else{
System.out.print("\t");
}
}else{
if(j==1||j==lineNum||j==lineNum-i+1){
System.out.print(ch+"\t");
}else{
System.out.print("\t");
}
// i-j: 5-3 6-2 7-1 j=lineNum-i+1
}
}
}
System.out.println();
}
}
效果:
第四个图形function:
public static void numberFour(int lineNum,char ch){
//判断奇数偶数
if(lineNum%2==0){
lineNum+=1;//变成奇数
}
for(int i=1; i<=lineNum;i++){
for(int j=1;j<=lineNum;j++){
if(i==1||i==lineNum){
System.out.print(ch+"\t");
}else{
if(j==1||j==lineNum||j==i||j==lineNum-i+1){
System.out.print(ch+"\t");
}else{
System.out.print("\t");
}
}
// System.out.print(ch+"\t");
}
System.out.println();
}
}
效果:
第五个图形function:
public static void nextclassOne(int lineNum,char ch){
int count=1;
//判断奇数偶数
if(lineNum%2==0){
lineNum+=1;//变成奇数
}
for(int i=1; i<=lineNum;i++){
for(int j=1;j<=lineNum;j++) {
if(i<=lineNum/2+1){
if (j == 1 || j == lineNum || j <= i || j >= lineNum - i + 1) {
System.out.print(count++ + "\t");
} else {
System.out.print("\t");
}
}else{
if (j == 1 || j == lineNum||j<=lineNum-i+1||j>=i) {
System.out.print(count++ + "\t");
} else {
System.out.print("\t");
}
}
}
System.out.println();
}
}
效果:
第六个图形function:
public static void nextclassTwo(int lineNum,char ch){
//判断奇数偶数
if(lineNum%2==0){
lineNum+=1;//变成奇数
}
for(int i=1; i<=lineNum;i++){
for(int j=1;j<=lineNum;j++) {
if(i==lineNum/2+1){
System.out.print(ch+"\t");
}else{
if(i<=lineNum/2+1){
if(j==lineNum/2+1||j==lineNum/2-i+2||j==lineNum/2+i){
System.out.print(ch+"\t");
}else{
System.out.print("\t");
}
}else{
if(j==lineNum/2+1||j==i-(lineNum/2+1)+1||j==lineNum/2+1+lineNum-i){
//j==i-(lineNum/2+1) 通过中轴和i j综合计算
System.out.print(ch+"\t");
}else{
System.out.print("\t");
}
}
}
}
System.out.println();
}
}
效果:
最后一个图形function:
public static void nextclassThree(int lineNum,char ch){
int count=1;
// System.out.print(printChar(count++));
//判断奇数偶数
if(lineNum%2==0){
lineNum+=1;//变成奇数
}
for(int i=1;i<=lineNum;i++){
for(int j=1;j<=lineNum;j++){
if(i==lineNum/2+1){
System.out.print(printChar(count++)+"\t");
}else{
if(i<lineNum/2+1){
if(j==lineNum/2+1||j==i||j==lineNum-i+1){
System.out.print(printChar(count++)+"\t");
}else{
System.out.print("\t");
}
}else if(i>lineNum/2+1){
if(j==lineNum/2+1||j==lineNum-i+1||j==i){
System.out.print(printChar(count++)+"\t");
}else{
System.out.print("\t");
}
}
}
}
System.out.println();
}
}
public static char printChar(int step){
//动态的改变打印的字符
int flag=step%3;
char ret=' ';
if(flag==1){
ret = '#';
}else if(flag==2){
ret= '$';
}else if(flag==0){
ret = '@';
}
return ret;
}
效果: