趣味練習:用迴圈語句做出圖形

蓋旭昇發表於2021-07-19

趣味練習:用迴圈語句做出圖形

 

 

             一、方形

             二、空心方形

             三、直角三角形

             四、倒直角三角形

             五、等腰三角形

             六、倒等腰三角形

             七、平行四邊形

             八、梯形

             九、菱形

             十、可調節菱形大小

             十一、等腰梯形

             十二、99乘法表

 

 

 

 

一、方形

指令碼命令

#!/bin/bash

for ((i=1; i<=10; i++))
do
  for ((a=1; a<=20; a++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

二、空心方形

指令碼命令

#!/bin/bash

for ((i=1; i<=10; i++))
do
  if [ $i -eq 1 -o $i -eq 10 ];then
    for ((a=1; a<=20; a++))
    do
      echo -n "*"
    done
    echo ""
  else
    echo -n "*"
    for ((b=1; b<=18; b++))
    do
      echo -n " "
    done
    echo "*"
  fi
done

 

 

三、直角三角形

指令碼命令

#!/bin/bash

for ((i=1; i<=9; i++))
do
  for ((a=1; a<=i; a++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

指令碼命令

#!/bin/bash

for ((i=1; i<=9; i++))
do
  for ((a=9; a>=i; a--))
  do
    echo -n " "
  done
  for ((b=1; b<=i; b++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

四、倒直角三角形

指令碼命令

#!/bin/bash

for ((i=1; i<=9; i++))
do
  for ((b=9; b>=i; b--))
  do
    echo -n "*"
  done
  echo ""
done

 

 

五、等腰三角形

指令碼命令

方法一

#!/bin/bash

for ((i=1; i<=9; i++))
do
  for ((a=9; a>=i; a--))
  do
    echo -n " "
  done
  for ((b=1; b<=i; b++))
  do
    echo -n "*"
  done
  for ((c=2; c<=i; c++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

方法二

指令碼命令

#!/bin/bash

for ((i=1; i<=9; i++))
do
  for ((a=9; a>=i; a--))
  do
    echo -n " "
  done

  num=$[($i*2) -1 ]
  for ((b=1; b<=num; b++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

六、倒等腰三角形

指令碼命令

#!/bin/bash

for ((i=9; i>=1; i--))
do
  for ((a=9; a>=i; a--))
  do
    echo -n " "
  done

  num=$[($i*2) -1 ]
  for ((b=1; b<=num; b++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

七、平行四邊形

指令碼命令

#!/bin/bash

for ((i=1; i<=6; i++))
do
  for ((a=1;a<=i; a++))
  do
    echo -n " "
  done
  for ((b=1; b<=6; b++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

指令碼命令

#!/bin/bash

for ((i=1; i<=6; i++))
do
  for ((a=6;a>=i; a--))
  do
    echo -n " "
  done
  for ((b=1; b<=6; b++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

八、梯形

指令碼命令

#!/bin/bash

for ((i=1; i<=6; i++))
do
  for ((a=6; a>=i; a--))
  do
    echo -n " "
  done
  for ((b=1; b<=6+$i; b++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

九、菱形

指令碼命令

 

#!/bin/bash

for ((i=1; i<=9; i++))
do
  for ((a=9; a>=i; a--))
  do
    echo -n " "
  done

  num=$[($i*2) -1 ]
  for ((b=1; b<=num; b++))
  do
    echo -n "*"
  done
  echo ""
done

for ((i=8; i>=1; i--))
do
  for ((a=9; a>=i; a--))
  do
    echo -n " "
  done

  num=$[($i*2) -1 ]
  for ((b=1; b<=num; b++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

十、可調節菱形大小

指令碼命令

#!/bin/bash

read -p "請輸入數字:" en
for ((i=1; i<=en; i++))
do
  for ((a=en; a>=i; a--))
  do
    echo -n " "
  done

  num=$[($i*2) -1 ]
  for ((b=1; b<=num; b++))
  do
    echo -n "*"
  done
  echo ""
done

for ((i=$[$en-1]; i>=1; i--))
do
  for ((b=en; b>=i; b--))
  do
    echo -n " "
  done

  num=$[($i*2) -1 ]
  for ((c=1; c<=num; c++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

十一、等腰梯形

指令碼命令

#!/bin/bash

for ((i=1; i<=6; i++))
do
  for ((a=6; a>=i; a--))
  do
    echo -n " "
  done
  for ((b=1; b<=6+$i; b++))
  do
    echo -n "*"
  done
  for ((c=1; c<=i; c++))
  do
    echo -n "*"
  done
  echo ""
done

 

 

十二、99乘法表

指令碼命令

#!/bin/bash

for ((a=1; a<=9; a++))
do
  for ((b=1; b<=a; b++))
  do
    c=$[$a*$b]
    echo -n -e "$a*$b=$c\t"
  done
  echo ""
done

 

 

 

相關文章