package test;
public class BendLine
{ static char[][] ch=new char[20][20];
static int length=ch.length-1;
public static void main(String[] args)
{
int x,y;
for(x=0;x<length+1;x++)//初始化字符数组
for(y=0;y<length+1;y++)
ch[x][y]=' ';
x=y=length;
int count=0;
while(count<(length+1)/2)
{
if(count%2==1)
area(x,y,'*');
else
area(x,y,' ');
x--;
y--;
count++;
}
for(x=0;x<length+1;x++)
{
System.out.print(ch[x]);
System.out.println("");
}
}
static void area(int x,int y,char c)
{
int i=length-x;
int j=length-y;
int n=0;
int k=1;
while(n<4)
{
if(n==0 && i<x)//n==0指左边
ch[i++][j]=c;
else if(n==0)
{n++;i=x;}
if(n==1 && j<y)//n==1指下边
ch[i][j++]=c;
else if(n==1)
{n++;j=y;}
if(n==2 && i>length-x)//n==2指右边
ch[i--][j]=c;
else if(n==2)
{n++;j=x;}
if(n==3 && j>length-x)//n==3指上边
ch[i][j--]=c;
else if(n==3)
{n++;}
}//while
if(ch[length-x+1][y]=='*')
ch[length-x+1][y]=' ';
else
ch[length-x+1][y]='*';
}
} |
|