# 使用星号创建沙漏[英] Creating an hourglass using asterisks

### 问题描述

```*****
***
*
***
*****
```

``` ***
*
***
```

```public static void draw(int W){
stars(W);
if (W > 1) {
draw(W-1);
stars(W);
}
}
public static void stars(int n){
System.out.print("*");
if(n>1) stars(n-1);
else System.out.println();
}
```

``` *****
****
***
**
*
**
***
****
```
<小时>

## 推荐答案

```public static void draw(int w){
draw(w, 0);
}

public static void draw(int W, int s){
stars(W, s);
if (W > 2) {
draw(W-2, s+1);
stars(W, s);
}
}
public static void stars(int n, int s){
if(s > 0){
System.out.print(" ");
stars(n, s-1);
} else  if (n > 0){
System.out.print("*");
stars(n-1, s);
} else {
System.out.println();
}
}
```

```for(int i = 1; i < 7; i++){
System.out.println("An hourglass of width " + i);
draw(i);
System.out.println();
}
```

```An hourglass of width 1
*

An hourglass of width 2
**

An hourglass of width 3
***
*
***

An hourglass of width 4
****
**
****

An hourglass of width 5
*****
***
*
***
*****

An hourglass of width 6
******
****
**
****
******
```