2015-05-04 44 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

相关问题