• linux 系统中while循环示例


    1、linux系统中while循环示例

    [root@linuxprobe test]# ls
    test.sh
    [root@linuxprobe test]# cat test.sh
    #!/bin/bash
    echo "the scale of price is 0-999!"  ## 输入内容提示
    PRICE=`expr $RANDOM % 1000`  ## 生成随机的范围变量,0-999
    COUNTS=0  ## 定义初始次数为0
    while true
    do
    read -p "please input your answer:" ANSWER
    let COUNTS++  ## 等价于 COUNTS=$[ $COUNTS + 1 ]
    if [ $ANSWER -eq $PRICE ]
    then
    echo "right,the price is $PRICE!"
    echo "you guess $COUNTS times!"
    exit 0 ## 等价于 exit / exit 1 / break
    elif [ $ANSWER -gt $PRICE ]
    then
    echo "high!"
    else
    echo "low!"
    fi
    done
    [root@linuxprobe test]# bash test.sh
    the scale of price is 0-999!
    please input your answer:500
    low!
    please input your answer:700
    low!
    please input your answer:800
    high!
    please input your answer:750
    high!
    please input your answer:730
    low!
    please input your answer:740
    high!
    please input your answer:735
    right,the price is 735!
    you guess 7 times!
  • 相关阅读:
    luogu 2962 [USACO09NOV]灯Lights
    bzoj 1923
    bzoj 1013
    bzoj 3513
    bzoj 4259
    bzoj 4503
    CF 632E
    bzoj 3527
    bzoj 3160
    bzoj 2179
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/13811781.html
Copyright © 2020-2023  润新知