feat: 添加奇偶判断函数

This commit is contained in:
LouisFonda 2024-07-14 14:59:43 +08:00
parent 845b012e72
commit a4d375af41
Signed by: yigencong
GPG Key ID: 36FE627068AA2092

View File

@ -0,0 +1,22 @@
#Program:
# 编写一个函数判断一个数的奇偶性
#History:
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
# 函数is_even
is_even () {
test $(expr $1 % 2) -eq 0 && return 0 || return 1
}
read -p "请输入一个整数:" num
# 判断这个数是否合法
if [ "$num" != "" ] && echo "$num" | grep -qE "^-?[0-9]+";then
if is_even $num;then
echo "这个数是偶数"
else
echo "这个数是奇数"
fi
else
echo "输入的数值不和法,请输入整数"
fi