小州老師您好
上課時練習的script第25_guess_games.sh- #!/bin/bash
- let n1=RANDOM%101
- let count=0
- start_time=$( date +%s )
- while true
- do
- echo -n "Please input the number to guess: "
- read n2
- let count++
- if [ -z "$n2" ]; then
- echo -e "\nError: try again !\n"
- elif [ "$n2" -gt "100" ] || [ "$n2" -lt "0" ]; then
- echo -e "\nError: out of range !\n"
- elif [ "$n2" -gt "$n1" ]; then
- echo -e "\nYour number $n2 is too big !\n"
- elif [ "$n2" -lt "$n1" ]; then
- echo -e "\nYour number $n2 is too small !\n"
- elif [ "$n2" -eq "$n1" ]; then
- end_time=$( date +%s )
- echo -e "\nBingo !"
- echo -e "\nYou use $count times, use $[ end_time - start_time ] seconds\n"
- echo -n "Play again [Y/N] ? "
- read again
- if [ "$again" == "y" ] || [ "$again" == "Y" ]; then
- let n1=RANDOM%101
- let count=0
- start_time=$( date +%s )
- continue
- else
- break
- fi
-
-
- else
- echo -e "\nError ! something wrong !\n"
- fi
- done
複製代碼 照老師的話練習把它改成丟一個數讓電腦來猜,我改成以下這樣- #!/bin/bash
- let n1=67
- let count=0
- while true
- do
- let n2=RANDOM%101
- let count++
- if [ "$n2" -eq "$n1" ] ; then
- echo -e "\nBingo!! n2=$n1\n"
- echo -e "\nPC run $count times\n"
- echo -n "Do you play again? (N)[ Y/N ]"
- read ans
- if [ "$ans" == "y" ] || [ "$ans" == "Y" ] ; then
- let n1=67
- let count=0
- else
- break
- fi
- elif [ "$n2" -gt "$n1" ] || [ "$n2" -lt "$n1" ] ; then
- echo -e "\n$n2 is a wrong number!"
- fi
- done
複製代碼 我的問題就是不知道是不是這樣改?還是根本就是錯的?或是有其他的建議可以讓我參考看看,謝謝。
我對寫程式實在沒有個底...如果問的太新請多多包涵 。 |