shell_猜数字游戏

#!/bin/bash
MAX=50
guess=-1

let answer=($RANDOM % $MAX)
let answer+=1

floor=1
ceiling=$MAX
guesstime=0

while [ "$guess" -ne "$answer" ]; do
 echo "The magic number is between $floor and $ceiling."
 echo -en "Make your guess:"
 read guess

 let guesstime+=1

 if [ "$guess" -lt "$answer" ]; then
  echo "$guess is too low"
  if [ "$guess" -gt "$floor" ]; then	
   let floor=($guess+1) 
  fi
 fi

 if [ "$guess" -gt "$answer" ]; then
  echo "$guess is too high"
  if [ "$guess" -lt "$ceiling" ]; then
   let ceiling=($guess-1)
  fi
 fi
done

echo "You have guessed $guesstime times"

阅读更多

没有更多推荐了,返回首页