public static String print(int n) {//打印n行的菱形
// TODO your code here
String res="";
int i = n%2;
int j = n/2;
if(n<0||i==0){
System.out.println("错误" + n);
return null;
}else{
for(int m=1;m<=j+1;m++){
String aaa = "";
for(int p=0;p<j+1-m;p++){
aaa+=" ";
}for(int q = 0; q<m*2-1;q++){
aaa += "*";
}
res =res + aaa + "\n";
}
for(int m=j+2;m<=n;m++){
String aaa = "";
for(int p=0;p<j+m-n;p++){
aaa+=" ";
}for(int q = 0; q<(n-m)*2 + 1;q++){
aaa += "*";
}
res =res + aaa + "\n";
}
}
System.out.println(res);
return res;
}
网友评论