我正在寫保齡球腳本的樂趣。我在幾個月前完成了一項家庭作業,並使用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
您不必重複標籤問題標題,標籤本身就夠了。 –