gpt4 book ai didi

linux - 为什么我的变量总是减为零?

转载 作者:太空宇宙 更新时间:2023-11-04 03:40:09 24 4
gpt4 key购买 nike

我正在写一个保龄球脚本来娱乐。几个月前我做了一个家庭作业,并让它在 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

关于linux - 为什么我的变量总是减为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30024639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com