猜数字的小游戏

运行程序后,提示用户输入一个0-9的数字,如果是非数字,那么就提示用户输入数字;如果用户猜中,提示用户猜对了;如果用户没有猜中,那么就提示用户重新输入一个数字;如果,用户连续五次都没有猜中,则提示用户,24小时后再来玩这个游戏;

#!/bin/bash
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 [ -n "$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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值