为什么我的变量总是递减到零?

时间:2015-05-04 07:20:53

标签: linux bash for-loop

我正在写一个有趣的保龄球剧本。几个月前我做了一个家庭作业,并用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))"

问题是,当我试图获得每个玩家的平均值时,平均值始终为零。我想仅在显示平均值时减少平均值VALUE。另一个问题是如果我没有递减价值,那么每位玩家在玩家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

1 个答案:

答案 0 :(得分:0)

这部分:

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

应该是:

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

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