문제

Convert the for-loop in the following script to a while-loop.

echo "Enter a number"
read n
count=0
for((i=1; i <= n; i++))
do
    echo "Enter a number"
    read a
    if((a > 0))
       then
       count=$((count+1))
    fi
done
echo "You entered $count positive number(s)"

my try:

while (i<=n)
echo "Enter a number"
read a
if ((a>0))
    then
    count=$((count+1))
i++

i dont know if i fully understand while and for loops. Thanks for your help ^_^

도움이 되었습니까?

해결책

Your loop is almost right; however you forgot to initialize i=1 before entering the while loop, and the do/done keywords

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top