Shell腳本實(shí)現(xiàn)猜數(shù)字游戲
更新時(shí)間:2020年04月21日 16:27:39 作者:zhangwei/7758258
這篇文章主要為大家詳細(xì)介紹了Shell腳本實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Shell實(shí)現(xiàn)猜數(shù)字游戲的具體代碼,供大家參考,具體內(nèi)容如下
這里的guem可以隨便寫(xiě),比如A、B之類的也可以
vim guem.sh
編輯腳本
#!/bin/bash
init()
{
echo " 猜數(shù)字游戲!!! "
}
game()
{
number=$[$RANDOM%100]
while :
do
read -p "輸入你要猜測(cè)的數(shù)字: " n1
n2=`echo $n1 | sed 's/[0-9]//g'`
if [ ! -z $n2 ]
then
echo "你輸入的不是一個(gè)數(shù)字."
continue
fi
if [ $n1 == $number ]
then
echo "你猜對(duì)了."
read -p "你還想再玩一次么?(yes/no)" an1
while true
do
case $an1 in
yes)
game
break
;;
no)
break
;;
*)
exit
esac
done
break
#!/bin/bash
init()
{
echo " "
echo " 猜數(shù)字游戲!!! "
}
game()
{
number=$[$RANDOM%100]
while :
do
read -p "輸入你要猜測(cè)的數(shù)字: " n1
n2=`echo $n1 | sed 's/[0-9]//g'`
if [ ! -z $n2 ]
then
echo "你輸入的不是一個(gè)數(shù)字."
continue
fi
if [ $n1 == $number ]
then
echo "你猜對(duì)了."
read -p "你還想再玩一次么?(yes/no)" an1
while true
do
case $an1 in
yes)
game
break
;;
no)
break
;;
*)
exit
esac
done
break
break
;;
no)
break
;;
*)
exit
esac
done
break
elif [ $n1 -gt $number ]
then
echo "猜大了."
continue
else
echo "猜小了."
continue
fi
done
}
while true
do
init
echo "1: 開(kāi)始游戲 "
echo "0: 退出游戲 "
read -p " 請(qǐng)輸入 0或者1:" NUM
case $NUM in
1)
game
break
;;
0)
break
;;
esac
done
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
linux定時(shí)任務(wù)出現(xiàn)command not found解決辦法
這篇文章主要介紹了 linux定時(shí)任務(wù)出現(xiàn)command not found解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05

