读入一些整数并判断奇数、偶数和0的个数

#! bin/bash

s=0

f=0

h=0

 

numb()

{

echo "please input the number: "    

read n

}

 

echo "please input the number of you read: "

read m

 

for i in $(seq 1 $m)     ----你希望输入整数的次数

do

numb                          ----调用函数输入整数

city[$i-1]=$n                ----将其存入一个数组

#let "k=$i-1"

#echo "city[$k]=${city[$i-1]}"

done

echo "${city[*]}"

 

for j in ${city[*]}       

do

  echo "$j"

  let "p=$j%2"

  if [ $p -eq 0 -a $j -ne 0 ]              ----偶数

  then

  let "s=$s+1"

 

  elif [ $p -eq 1 ]                        ----奇数

   then

   let "f=$f+1"

 

  else

     let "h=$h+1"                         -----0

  fi

done

 

echo "oushu number is $s"

echo "jishu number is $f"

echo "0 number is $h"






原来read有读取多个数的功能,哎自己才晓得!!!上面的就写的太麻烦了,简单版
 #! bin/bash
s=0
f=0
h=0
sum1=0
sum2=0
sum3=0

echo "please input the num of you read: "
read m

for i in $m
do
 if [ $(($i%2)) -eq 0 -a $i -ne 0 ]
  then
  let "s=$s+1"
let "sum1=$sum1+$i"
 elif [ $(($i%2)) -eq 1 ]
  then
   let "f=$f+1"
let "sum2=$sum2+$i"
 else
   let "h=$h+1"
let "sum3=$sum3+$i"
 fi


done
echo "oushu ge shu is $s"
echo "oushu sum is $sum1"
echo "0 ge shu is $h" 
echo "jishu ge shu is $f"
echo "jishu sum is $sum2"

   

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29096438/viewspace-1431552/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29096438/viewspace-1431552/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值