linux 中while循环实现猜数字游戏

 

1、

复制代码
[root@rhel7pc1 test]# cat test.sh
#!/bin/bash
NUM=$[RANDOM % 100 + 1]
TIMES=0
while :
  do
        read -p "please input a 1~100 num: " ANS
        TIMES=$[TIMES+1]
        if [ $ANS -eq $NUM ]
        then
                echo "yes, your answer is right!"
                echo "you had guessed $TIMES times!!"
                exit
        elif [ $ANS -gt $NUM ]
        then
                echo "too big!"
        else
                echo "too small!"
        fi
done
复制代码
复制代码
[root@rhel7pc1 test]# ls
test.sh
[root@rhel7pc1 test]# bash test.sh
please input a 1~100 num: 50
too small!
please input a 1~100 num: 80
too big!
please input a 1~100 num: 60
too small!
please input a 1~100 num: 70
too big!
please input a 1~100 num: 65
too small!
please input a 1~100 num: 67
too big!
please input a 1~100 num: 66
yes, your answer is right!
you had guessed 7 times!!
复制代码

 

posted @   小鲨鱼2018  阅读(234)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-04-16 R语言中fig函数,图形布局的精细控制
2021-04-16 R语言中layout图形布局函数
2021-04-16 R语言中text函数
2021-04-16 R语言中使用locator(1)函数获取图形的位置坐标
2021-04-16 R语言中给图形添加图例
2021-04-16 R语言绘图abline函数 给图形增加参考线
2021-04-16 R语言绘图添加次要刻度线
点击右上角即可分享
微信分享提示