如何利用循环打印出如下效果?
温馨提示:答案为网友推荐,仅供参考
第1个回答 2012-11-12
这个不知道是不是你要的效果,看你那图没什么规律啊:
[seesea2517 ~] $ ./a.sh 1
+
[seesea2517 ~] $ ./a.sh 2
+
+++
[seesea2517 ~] $ ./a.sh 3
+
+++
+++++
[seesea2517 ~] $ ./a.sh 4
+
+++
+++++
+++++++
[seesea2517 ~] $ cat a.sh
#!/bin/bash
if [ -z "$1" ]
then
echo "需要参数一,表示高度。"
exit
fi
for (( i = 1; i <= $1; ++i ))
do
line=""
for (( j = 0; j < 2 * i - 1; ++j ))
do
line="$line+"
done
echo "$line"
done
不知道你是不是截图不对,所以还有另一种效果:
[seesea2517 ~]$ ./a.sh 1
+
[seesea2517 ~]$ ./a.sh 2
+
+++
[seesea2517 ~]$ ./a.sh 3
+
+++
+++++
[seesea2517 ~]$ ./a.sh 6
+
+++
+++++
+++++++
+++++++++
+++++++++++
[seesea2517 ~]$ cat a.sh
#!/bin/bash
if [ -z "$1" ]
then
echo "需要参数一,表示高度。"
exit
fi
for (( i = 1; i <= $1; ++i ))
do
line=""
for (( j = $1; j >= i; --j ))
do
line="$line "
done
for (( j = 0; j < 2 * i - 1; ++j ))
do
line="$line+"
done
echo "$line"
done
[seesea2517 ~] $ ./a.sh 1
+
[seesea2517 ~] $ ./a.sh 2
+
+++
[seesea2517 ~] $ ./a.sh 3
+
+++
+++++
[seesea2517 ~] $ ./a.sh 4
+
+++
+++++
+++++++
[seesea2517 ~] $ cat a.sh
#!/bin/bash
if [ -z "$1" ]
then
echo "需要参数一,表示高度。"
exit
fi
for (( i = 1; i <= $1; ++i ))
do
line=""
for (( j = 0; j < 2 * i - 1; ++j ))
do
line="$line+"
done
echo "$line"
done
不知道你是不是截图不对,所以还有另一种效果:
[seesea2517 ~]$ ./a.sh 1
+
[seesea2517 ~]$ ./a.sh 2
+
+++
[seesea2517 ~]$ ./a.sh 3
+
+++
+++++
[seesea2517 ~]$ ./a.sh 6
+
+++
+++++
+++++++
+++++++++
+++++++++++
[seesea2517 ~]$ cat a.sh
#!/bin/bash
if [ -z "$1" ]
then
echo "需要参数一,表示高度。"
exit
fi
for (( i = 1; i <= $1; ++i ))
do
line=""
for (( j = $1; j >= i; --j ))
do
line="$line "
done
for (( j = 0; j < 2 * i - 1; ++j ))
do
line="$line+"
done
echo "$line"
done