如何打印三个数字(输入)的平均值和最大数量(输入)

时间:2016-08-23 13:02:08

标签: bash shell unix

到目前为止,我只能得到平均数字,但数量最多。

#! /bin/sh
# Get input numbers from user
 echo Please enter three numbers
 read a b c
# Calculate average of input numbers
avg=`expr \( $a + $b + $c \) / 3`
# Display the result
echo The average of the two input numbers is $avg
exit

1 个答案:

答案 0 :(得分:0)

试试这个;

#! /bin/sh
# Get input numbers from user
 echo Please enter three numbers
 read a b c
# Calculate average of input numbers
avg=`expr \( $a + $b + $c \) / 3`


for i in $a $b $c ; do
    ((i > max)) && max=$i
done

echo The maximum number of the three input numbers is $max
# Display the result
echo The average of the three input numbers is $avg
exit