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!!
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!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语言绘图添加次要刻度线