unix 编写一个shell脚本来查找三个数字中的最大值,获取用户输入并显示结果

ego6inou  于 2022-11-04  发布在  Unix
关注(0)|答案(8)|浏览(410)

编写一个shell脚本来查找三个数字中的最大值。获取用户输入并显示结果。
样本输入1:

10
20
30

示例输出1:

30 is largest number

样本输入2:

10
10
10

示例输出2:

All the three numbers are equal

样本输入3:

10
10
1

示例输出3:

I cannot figure out which number is largest

Bash代码:


# !bin/bash

read a b c
if [ $a -eq $b -a $a -eq $c ]; then
    echo "All the three numbers are equal"
elif [[ $a -eq $b || $b -eq $c || $c -eq $a ]]; then
    echo "I cannot figure out which number is largest"
else
    if [ $a -gt $b -a $a -gt $c ]; then
        echo "$a is biggest number"
    elif [ $b -gt $a -a $b -gt $c ]; then
        echo "$b is biggest number"
    elif [ $c -gt $a -a $c -gt $b ]; then
        echo "$c is biggest number"
    fi
fi

要通过的测试用例:有三个测试用例需要通过,但是我只能通过一个测试用例。
1.不同的值(通过)。

1.唯一值(失败)。

1.相等值(失败)。

评估代码时的实际结果:

注:尽管我得到的实际结果与问题中提到的预期结果相同,但我能够通过唯一一个测试用例(不同值),其余两个测试用例(相等值和唯一值)失败。我甚至无法发现错误。
当我尝试这些方法时,我遇到了一些错误:
第一个

xpszyzbs

xpszyzbs1#

通过所有测试用例

` 
read n1
read n2
read n3

if (( $n1==$n2 & $n2!=$n3 ))
then
  echo "I cannot figure out which number is largest"
elif (( $n1==$n2  &  $n1==$n3 ))
then 
 echo "All the three numbers are equal"
 else
   if (( $n1>=$n2 & $n1>=$n3 ))
   then
    echo "$n1 is largest number"
   elif (( $n2>=$n1 & $n2>=$n3 ))
   then
    echo "$n2 is largest number"
   else
    echo "$n3 is largest number"
 fi
fi`
kzipqqlq

kzipqqlq2#

read n1
read n2
read n3
if [[ $n1 == $n2 && $n2 == $n3 ]]; then #Note : There should be space between $variable and == operator
    echo "All the three numbers are equal"
elif [[ $n1 == $n2 || $n2 == $n3 || $n1 == $n3 ]]; then
    echo "I cannot figure out which number is largest"
elif [[ $n1 > $n2 && $n1 > $n3 ]]; then
    echo "$n1 is largest number"
elif [[ $n2 > $n1 && $n2 > $n3 ]]; then
    echo "$n2 is largest number"
else
    echo "$n3 is largest number"
fi
r9f1avp5

r9f1avp53#

接受任意三个数字,使用if循环比较它们,使用echo

打印输出

h22fl7wq

h22fl7wq4#

read n1
read n2
read n3
if [[ $n1 == 0 || $n2 == 0 || $n3 == 0 ]]; then
    echo "command line arguments are missing"
elif [[ $n1 == $2 && $n2 == $n3 ]]; then
    echo "All the three numbers are equal"
elif [[ $n1 == $n2 || $n2 == $n3 || $n3 == $n1 ]]; then
    echo "I cannot figure out which number is biggest"
else
    if [[ $n1 > $n2 && $n1 > $n3 ]]; then 
        echo "$n1 is Biggest number"
    elif [[ $n2 > $n1 && $n2 > $n3 ]]; then
        echo "$n2 is Biggest number"
    else
        echo "$n3 is Biggest number"
    fi
fi
3npbholx

3npbholx5#

错误不在您的代码中。它在echo语句中。您必须打印Accenture在描述中要求的确切语句。它应该是最大数而不是最大数。
我也被困在这个令人沮丧的问题上,终于意识到哪里出了问题。

pokxtpni

pokxtpni6#

程序逻辑有问题。根据您的代码,如果用户输入(10,10,1),它将返回“I can't find out which number is greater”(我找不到哪个数字更大),这对于该特定条目是正确的,但如果用户输入(20,10,10),您的代码仍将返回“I can't find out blah blah”(我找不到什么),而应该返回“20 is maximum number”(20是最大数字)。

4ngedf3f

4ngedf3f7#


# !/bin/bash

read num1
read num2
read num3
if [ $num1 == $num2 -a $num1 == $num3 ]
then
 echo "All the three numbers are equal"
elif [ $num1 == $num2 -o $num1 == $num3 -o $num2 == $num3 ]
then
 echo "I cannot figure out which number is largest"
else
 if [ $num1 -gt $num2 -a $num1 -gt $num3 ]
 then
   echo "$num1 is largest numer"
 elif [ $num2 -gt $num1 -a $num2 -gt $num3 ]
 then
   echo "$num2 is largest number"
 else
   echo "$num3 is largest number"
 fi
fi
h79rfbju

h79rfbju8#

我也遇到了同样的问题,花了点时间排除故障。

read v1
read v2
read v3

if [[ $v1 == $v2 && $v1 == $v3 ]]; then
    echo "All the three numbers are equal"
elif [[ $v1 == $v2 || $v2 == $v3 || $v3 == $v1 ]]; then
    echo "I cannot figure out which number is largest"
else
    if [[ $v1 > $v2 && $v1 > $v3 ]]; then
        echo "$v1 is largest number"
    elif [[ $v2 > $v3 && $v2 > $v1 ]]; then
        echo "$v2 is largest number"
    elif [[ $v3 > $v1 && $v3 > $v2 ]]; then
        echo "$v3 is largest number"
    fi
fi

相关问题