我正在写保龄球脚本。几个月前,我做了一个家庭作业,并在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
此部分:
(( bowlingScores +- REPLY ))
((teamPlayer-1))
((bowlingGame-1))
应该是:
(( bowlingScores += REPLY ))
((teamPlayer-=1))
((bowlingGame-=1))
检查:http://tldp.org/ldp/abs/html/arithexp.html