编写猜价格游戏

编写猜价格游戏

9、编写猜价格游戏

参考脚本如下

 1 #!/bin/bash
 2 #编写猜价格游戏
 3 
 4 #定义正确价格
 5 Prnum=`expr $RANDOM % 20`              #获取一个1-20之间的随机数
 6 b=0
 7 
 8 while true
 9 do
10   read -p "请输入价格(1-20):" Price    #输入价格
11   b=b+1
12   if [ $Price -eq $Prnum ];then        #判断输入的价格是否=随机生成的数字
13      echo "恭喜你,猜对啦!"
14      echo "价格是:$Price"
15      exit 0
16   elif [ $Price -gt $Prnum ];then
17      echo "太高了"
18   else
19      echo "太低了"
20   fi
21 done