1
我對我的UNIX編號猜測遊戲存在邏輯問題。菜單系統工作得很好(1玩,2退出,否則防守反應)。但是,一旦在比賽中我得到的輸出如下:UNIX編號猜測遊戲 - 邏輯問題
- 選項:
- (1.播放)
- (2號出口)
- 輸入數字1或2
- 請輸入你猜到的。
- pgm01 [43]:[5:沒有發現[沒有這樣的文件或目錄]
- pgm01 [47]:[5:沒有發現[沒有這樣的文件或目錄]
- 耶!! ! 5是正確的數字!
- 你嘗試的總數爲0。
下面是代碼(FYI這是在KSH殼)。我猜我有某種語法錯誤的地方...
#!/bin/ksh
# CS2351 - UNIX Programming
# Program 1
x=$RANDOM%100+1
tries=0
guessInt=0
userOption=0
while ((1==1))
do
print "Welcome to the UNIX Number Guessing Game!"
print "A random number between 1 and 20 has been selected."
print "===========\nDirections: \n==========="
print "1. Enter your guess. The program will tell you if it is high or low."
print "2. The program will tell you if it is high or low."
print "3. Change your guess."
print "4. The computer will tell you how many guesses you had."
print "=======\nOPTIONS\n======="
print "1: Play \n2: Exit"
print "Enter number 1 or 2."
"Enter number 1 or 2."
read userinput
case "$userinput" in
1)
break;;
2)
print "Quitting program!!!"
exit;;
*)
print "The input $userinput is invalid."
print "Returning to main menu..."
print "\n\n\n";;
esac
done
print "Please enter you're guess."
read guessInt
while (($guessInt != "q"));
do
if [$guessInt -lt $x];
then
print "Your guess is too low, try again! (q to quit)."
tries=tries+1
elif [$guessInt -gt $x];
then
print "Your guess is too high, try again! (q to quit)."
tries=tries+1
else
print "Yay!!! $guessInt is the right number!!!"
break
fi
done
print "Your total number of tries were $tries."
exit
任何深入瞭解這個問題,表示讚賞!
謝謝,你的指導幫助了很多! – gmanjake 2012-03-27 23:16:19