guess number
cat guess
#!/bin/bash
MAX=50
guess=1
let answer=($RANDOM % $MAX)
let answer+=1
ceiling=$MAX
floor=0
guesses=0
while [[ “$guess” != “$answer” ]]
do
echo “The magic number is between $floor and $ceiling.”
echo -en “ Make your guess:”
read guess
guesses=`expr $guesses + 1`
if [[ “$guess” < “$answer” ]]; then
echo “$guess is too low”
if [[ “$guess” > “$floor” ]]; then
floor=`expr $guess + 1`
fi
fi
if [[ “$guess” > “$answer” ]]; then
echo “$guess is too high”
if [[ “$guess” < “$ceiling” ]]; then
ceiling=`expr $guess - 1`
fi
fi
done
echo “You got it in $guesses guesses!”
---------
bash guess
“The magic number is between 0 and 50.”
“ Make your guess:”33
“33 is too low”
“The magic number is between 34 and 50.”
“ Make your guess:”40
“40 is too low”
“The magic number is between 41 and 50.”
“ Make your guess:”46
“46 is too low”
“The magic number is between 47 and 50.”
“ Make your guess:”49
“49 is too high”
“The magic number is between 47 and 48.”
“ Make your guess:”47
“You got it in 5 guesses!”
#!/bin/bash
MAX=50
guess=1
let answer=($RANDOM % $MAX)
let answer+=1
ceiling=$MAX
floor=0
guesses=0
while [[ “$guess” != “$answer” ]]
do
echo “The magic number is between $floor and $ceiling.”
echo -en “ Make your guess:”
read guess
guesses=`expr $guesses + 1`
if [[ “$guess” < “$answer” ]]; then
echo “$guess is too low”
if [[ “$guess” > “$floor” ]]; then
floor=`expr $guess + 1`
fi
fi
if [[ “$guess” > “$answer” ]]; then
echo “$guess is too high”
if [[ “$guess” < “$ceiling” ]]; then
ceiling=`expr $guess - 1`
fi
fi
done
echo “You got it in $guesses guesses!”
---------
bash guess
“The magic number is between 0 and 50.”
“ Make your guess:”33
“33 is too low”
“The magic number is between 34 and 50.”
“ Make your guess:”40
“40 is too low”
“The magic number is between 41 and 50.”
“ Make your guess:”46
“46 is too low”
“The magic number is between 47 and 50.”
“ Make your guess:”49
“49 is too high”
“The magic number is between 47 and 48.”
“ Make your guess:”47
“You got it in 5 guesses!”
Comments
Post a Comment