亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

正在回答

4 回答

#!/bin/bash
echo?"Hi,?this?is?a?calculater"
read?-t?30?-p?"Please?input?num1:"?num1
read?-t?30?-p?"Please?input?num2:"?num2
read?-t?30?-p?"Please?input?the?operator(+-*/):"?ope

if?[?-z?"$num1"?-o?-z?"$num2"?-o?-z?"$ope"?]
????then
????????echo?"Sorry,?Missed?input"
????????exit?12
fi

test1=$(echo?$num1?|?sed?'s/[0-9]//g')
test2=$(echo?$num2?|?sed?'s/[0-9]//g')
if?[?-n?"$test1"?-o?-n?"$test2"?]
????then
????????echo?"Sorry,?Invalid?Value"
????????exit?11
fi

if?[?"$ope"?==?"+"?]
????then
????????res=$(($num1+$num2))
????????echo?"+?operated"
elif?[?"$ope"?==?"-"?]
????then
????????res=$(($num1-$num2))
????????echo?"-?operated"
elif?[?"$ope"?==?"*"?]
????then
????????res=$(($num1*$num2))
????????echo?"*?operated"
elif?[?"$ope"?==?"/"?]
????then
????????if?[?"$num2"?-eq?"0"?]
????????????then
????????????????echo?"Sorry,?Can?not?do?this,?Bye"
????????????????exit?10
????????else
????????????res=$(($num1/$num2))
????????????echo?"/?operated"
????????fi
else
????echo?"Sorry,?Invalid?Operator"
????exit?9
fi

echo?"$num1?$ope?$num2?:?$res"
echo?"Thanks"


1 回復 有任何疑惑可以回復我~
#1

慕妹2006075 提問者

非常感謝!
2015-10-07 回復 有任何疑惑可以回復我~
?#!/bin/bash
??2?
??3?read?-p?"Please?input?your?firstnum:"??num1
??4?read?-p?"Please?input?your?secondnu:"??num2
??5?read?-p?"please?input?your?operation:"?ope
??6?test1=$(echo?$num1?|?grep?"[^0-9]")
??7?test2=$(echo?$num2?|?grep?"[^0-9]")
??8?if?[?-z?"$num1"?-o?-z?"$num2"?-o?-z?"$ope"?]
??9?????????then
?10?????????????????echo?"Input?is?NULL"
?11?????????????????exit?10
?12?fi
?13?if?[?-n?"$test1"?-o?-n?"$test2"?]
?14?????????then
?15?????????????????echo?"Input?is?not?number"
?16?????????????????exit?11
?17?fi
?18?
?19?if?[?"$ope"?==?'+'?]
?20?????????then
?21??????????result=$(($num1?+?$num2))
?22?elif?[?"$ope"?==?'-'?]
?23?????????then
?24??????????result=$(($num1?-?$num2))
?25?elif?[?"$ope"?==?'*'?]
?26?????????then
?27??????????result=$(($num1?*?$num2))
?28?elif?[?"$ope"?==?'/'?]
?29?????????then
?30??????????result=$(($num1?/?$num2))
?31?else
?32?????????echo?"Ope?is?error"
?33?????????exit?12
?34?fi
?35?echo?$result


0 回復 有任何疑惑可以回復我~

? #我這個方法比老師的簡單,判斷空不用那么麻煩

?1 #!/bin/bash

? 2?

? 3 read -p "Please input your firstnum:" ?num1

? 4 read -p "Please input your secondnu:" ?num2

? 5 read -p "please input your operation:" ope

? 6 test1=$(echo $num1 | grep "[^0-9]")

? 7 test2=$(echo $num2 | grep "[^0-9]")

? 8 if [ -z "$num1" -o -z "$num2" -o -z "$ope" ]

? 9 ? ? ? ? then

?10 ? ? ? ? ? ? ? ? echo "Input is NULL"

?11 ? ? ? ? ? ? ? ? exit 10

?12 fi

?13 if [ -n "$test1" -o -n "$test2" ]

?14 ? ? ? ? then

?15 ? ? ? ? ? ? ? ? echo "Input is not number"

?16 ? ? ? ? ? ? ? ? exit 11

?17 fi

?18?

?19 if [ "$ope" == '+' ]

?20 ? ? ? ? then

?21 ? ? ? ? ?result=$(($num1 + $num2))

?22 elif [ "$ope" == '-' ]

?23 ? ? ? ? then

?24 ? ? ? ? ?result=$(($num1 - $num2))

?25 elif [ "$ope" == '*' ]

?26 ? ? ? ? then

?27 ? ? ? ? ?result=$(($num1 * $num2))

?28 elif [ "$ope" == '/' ]

?29 ? ? ? ? then

?30 ? ? ? ? ?result=$(($num1 / $num2))

?31 else

?32 ? ? ? ? echo "Ope is error"


0 回復 有任何疑惑可以回復我~

這個實在是太簡單了。

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

if 怎樣改成非嵌套

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號