#!/bin/bash
target=$[ $RANDOM%100+1 ]
times=0
echo "请输入猜想的数字:"
while true
do
read number
while true #循环控制保证输入的是数字,若非数字提示并要求重新输入
do
if [ "$number" -gt 0 ] 2>/dev/null ;
then
break
else
echo "输入的不是数字,请重新输入:"
read number
fi
done
if [ $number -eq $target ]
then
echo "猜对了!"
let times=$times+1
echo '本次猜对所用次数为:'$times
echo "是否要再来一局[y/n]"
times=0
target=$[ $RANDOM%100+1 ]
read choice
case $choice in
y|Y|yes)
echo "请输入猜想的数字:"
continue
;;
n|N|no)
echo "退出游戏"
exit
;;
*)
echo "输入错误,退出游戏"
break
;;
esac
elif [ $number -gt $target ]
then
let times=$times+1
echo "猜大了!"
else
let times=$times+1
echo "猜小了!"
fi
done