2015-05-04 54 views
0

我正在寫保齡球腳本的樂趣。我在幾個月前完成了一項家庭作業,並使用C++工作,但現在我想用bash來做,因爲我更喜歡它。爲什麼我的變量總是遞減到零?

這裏是我的代碼:

#!/bin/bash 

for ((teamPlayer = 1 ; teamPlayer <= 5; teamPlayer++)); do 

     for ((bowlingGame = 1; bowlingGame <=3; bowlingGame++)); do 

      read -p "Please enter the bowling score for Player number $teamPlayer in game number $bowlingGame: " 

        while [[ -z $REPLY ]] || (($REPLY > 300 || $REPLY < 1)) || [[ ! $REPLY =~ [[:digit:]] ]]; do 

         if ((teamPlayer >=1)); then 
          ((bowlingScores +- REPLY)) 
          ((teamPlayer-1)) 
          ((bowlingGame-1)) 
         fi 

         echo -e "\nError Try Again!" 

         read -p "Please enter the bowling score for Player number $teamPlayer in game number $bowlingGame: " 
        done 

      ((bowlingScores += REPLY)) 

    done 

    ((bowlingScores += average)) 

    echo "The average for player number $teamPlayer is $((average/3))" 

    ((average--)) 

done 

echo "The average score for the team is $((bowlingScores/15))" 

問題是,當我試圖讓平均每個球員的平均始終爲零。我只想在顯示平均值時減少平均值。另一個問題是如果我不減少價值,玩家1之後的每個玩家得到一個錯誤的平均值。

任何幫助將不勝感激。

編輯:得到它的工作。這是新的代碼。

#!/bin/bash 

for ((teamPlayer = 1 ; teamPlayer <= 5; teamPlayer++)); do 

     for ((bowlingGame = 1; bowlingGame <=3; bowlingGame++)); do 

      read -p "Please enter the bowling score for Player number $teamPlayer in game number $bowlingGame: " 


        while [[ -z $REPLY ]] || (($REPLY > 300 || $REPLY < 1)) || [[ ! $REPLY =~ [[:digit:]] ]]; do 

         if ((teamPlayer >=1 || bowlingGame >=1)); then 
          ((bowlingScores +- REPLY)) 
          ((teamPlayer >=1)) && ((teamPlayer-1)) 
          ((bowlingGame-1)) && ((teamPlayer-1)) 
         fi 

         echo -e "\nError Try Again!" 

         read -p "Please enter the bowling score for Player number $teamPlayer in game number $bowlingGame: " 
        done 

      ((bowlingScores += REPLY)) 


      ((average += REPLY)) 

    done 



    echo "The average for player number $teamPlayer is $((average/3))" 

    average=0 

done 

echo "The average score for the team is $((bowlingScores/15))" 

echo $bowlingScores 
+0

您不必重複標籤問題標題,標籤本身就夠了。 –

回答

0

這一部分:

((bowlingScores +- REPLY)) 
((teamPlayer-1)) 
((bowlingGame-1)) 

應該是:

((bowlingScores += REPLY)) 
((teamPlayer-=1)) 
((bowlingGame-=1)) 

檢查:http://tldp.org/LDP/abs/html/arithexp.html

+0

這個答案內容是真實的。另外請注意,bash只能執行整數運算。如果你(OP)期待浮點數作爲答案,你就不會通過簡單的劃分來得到它。 – anishsane

+0

謝謝klashxx。在我回到網站之前,我已經修復了第一部分。我一定會看看鏈接。 – quasifilmie

+0

但是,我不認爲變量teamPlayer和bowlingGame應該變成 - = 1。我只想減少他們,如果他們等於1.我會發布我的新代碼。 – quasifilmie

相關問題