所以是的,我正在为学校做这件事,但我写了大部分剧本。我不知道它发生了什么,可能是语法错误,但它一直在搞砸(或者我这样做)。
第一个问题是它不断发布您试图猜测的数字并且它说没有文件或目录(我不认为我在呼吁这样的事情)。86 是当前的随机数。
./random: line 14: 86: No such file or directory
第二个问题是程序告诉我猜测总是太低(我也可以让它们总是太高)
I'm thinking of a number between 1 and 100. Your guess:6
./random: line 14: 86: No such file or directory
Sorry, your guess is too low. New guess:87
./random: line 14: 86: No such file or directory
Sorry, your guess is too low. New guess:
这是我的代码:
#!/bin/bash
n1=$[($RANDOM % 100) +1]
guesses=1
echo -n "I'm thinking of a number between 1 and 100. Your guess:"
while read n2; do
if [ $n2 = $n1 ]; then
break;
else
echo
if [ $n2 < $n1 ]; then
echo -n "Sorry, your guess is too high. New guess:"
elif [ $n2 > $n1]; then
echo -n "Sorry, your guess is too low. New guess:"
fi
fi
guesses=$((guesses+1))
done
echo
echo "Good job! It took you $guesses guesses to get the right number."
提前致谢。