杨海脚本练习题答案

回复 收藏
帖子地址: http://www.apelearn.com/bbs/thread-7226-1-1.html
还不是很完美,因为没有判断,如果输入的数字不是1-10之间。大家补充一下吧。
  1. #!/bin/bash
  2. n2=6
  3. while :
  4. do
  5.     read -p "Please input a number(1-10): " n
  6.     n1=`echo $n|sed 's/[0-9]//g'`
  7.     if [ ! -z $n1 ]
  8.     then
  9.         tag=2
  10.         echo "Just number."
  11.     elif [ $n -gt 10 ] || [ $n -lt 1 ]
  12.     then
  13.         tag=1
  14.         echo "The number must >1 and <10."
  15.     else
  16.         tag=0
  17.     fi
  18.     if [ $tag -ne 0 ]
  19.     then
  20.         c=0
  21.         continue
  22.     elif [ $n -ne $n2 ]
  23.     then
  24.         echo "Wrong."
  25.         c=$[$c+1]
  26.         if [ $c -eq 5 ]
  27.         then
  28.             echo "Wait 24 hours."
  29.             exit 1
  30.         else
  31.             continue
  32.         fi
  33.     else
  34.         echo "Right."
  35.         exit 0
  36.     fi
  37. done

2015-06-29 22:32 举报
已邀请:
0

定海偶然

赞同来自:

啊,出来了
0

九月微寒

赞同来自:

本帖最后由 九月微寒 于 2015-6-29 23:49 编辑

  1. #!/bin/bash
  2. # liusheng @20150629
  3. num1=$(($RANDOM%10));
  4. for ((i=1;i<=5;i++))
  5. do
  6.     read -p "猜数字,共五次机会,这是第 $i 次机会。请输入0-9的数字 ? " num
  7.     n=`echo $num |sed 's/[0-9]//g'`
  8.     if [ -z $n ];then
  9.         [ "$num" == "$num1" ] && echo "你猜对了!" && exit
  10.     else
  11.         echo "你输入的是数字么?" && let i=$i-1 && continue
  12.     fi
  13. done
  14. echo "24小时后来吧!"

0

赞同来自:

  1. #!/bin/bash
  2. lucky=8
  3. c=0
  4. while :
  5. do
  6.     read -p "Please input a number [0-9]: " num
  7.     n1=`echo $num|sed 's/[0-9]//g'`
  8.     if [ ! -z $n1 ]
  9.     then
  10.        continue
  11.     elif [ -z $num ]
  12.     then
  13.         continue
  14.     elif [ $num -gt 10 ]
  15.     then
  16.         continue
  17.     else
  18.         if [ $num -eq $lucky ]
  19.         then
  20.             echo "niu b,you win!"
  21.             exit 0
  22.         else
  23.             c=$[$c+1]
  24.             if [ $c -eq 5 ]
  25.             then
  26.                 echo "Failed five times,please try again 24h later."
  27.                 exit 0
  28.             fi
  29.         fi
  30.     fi
  31. done

0

qq20847697

赞同来自:

本帖最后由 qq20847697 于 2015-6-30 21:59 编辑

终于写好了,24小时改成了5秒,感觉成功了:

  1. #!/bin/bash
  2. check_num(){
  3. a=`echo $1|sed 's/^[0-9]\{1\}//g'`
  4. if [ ! -z $1 ] && [ -z $a ]
  5. then
  6.   return 0
  7. else
  8.   return 1
  9. fi
  10. }

  11. echo_num(){
  12. stat=1
  13. while [ $stat == 1 ]
  14. do
  15. read -p"请输入一个范围在0-9的数字:" n
  16. check_num $n
  17. stat=`echo $?`
  18. done
  19. m=`echo $(($RANDOM%10))`
  20. }

  21. tag=0
  22. while :;do
  23. echo_num
  24. if [ $n -eq $m ]
  25. then
  26.   echo " 你输入的数字是:$n,非常好,你猜对了!"
  27. break
  28. else
  29.   tag=$[$tag+1]
  30.   gat=$[5-$tag]
  31. if [ $tag -ge 5 ];then
  32.   echo "你猜错了5次,请24小时后再试!"
  33.   sleep 5
  34.   tag=0
  35. else
  36.   echo "你猜错了,你还有$gat次机会,请重新输入!"
  37. fi
  38. fi

  39. done
0

Linuxpp

赞同来自:

study
0

渐行渐远

赞同来自:

tag=0是啥意思
0

黄国斌

赞同来自:

回复
0

kevinjin

赞同来自:

#! /bin/bash
sum=0
read -p "Please input a number: " n1
if [ -z `echo $n1|sed 's/[0-9]//'` ]
then
    echo "you got it right"
else
    while [ $sum -lt 5 ]
    do
        read -p "Your input is not a number, please input a number again: " n2
        [ -z `echo $n2|sed 's/[0-9]//'` ] || sum=$[$sum+1]
    done
    echo "Please try again after 24 hours"
fi
0

王斌

赞同来自:

#!/bin/bash
# auth: Cityzen Wang
function check() {
    if [ $c -eq 5 ]
        then
            echo "Wait 24 hours."
            exit 1
        else
            continue
        fi
}
n2=6
c=0
while :
do
    read -p "Please input a number(1-10): " n
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ ! -z $n1 ]
    then
        tag=2
        echo "Just number."
        c=$[$c+1]
        check
    elif [ $n -gt 10 ] || [ $n -lt 1 ]
    then
        tag=1
        c=$[$c+1]
        check
        echo "The number must great than 1 and less than 10."
    else
        tag=0
        c=$[$c+1]
        check
    fi
    if [ $tag -ne 0 ]
    then
        continue
    elif [ $n -ne $n2 ]
    then
        echo "Wrong."
        c=$[$c+1]
        check
    else
        echo "Right."
        exit 0
    fi
done


回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: