三种分奇数偶数的方法

第一种:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> lt = new List<int>();//容器
            List<int> lt1 = new List<int>();//偶数容器
            List<int> lt2 = new List<int>();//奇数容器
            int opp = 0;//偶数个数
            int od = 0;//奇数个数
            string str;//输入十个整数

            Console.WriteLine("请输入十个整数:");

            while ((str = Console.ReadLine())!=null)
            {
                try
                {
                    string[] strtemp = str.Split(' ');//分解

                    foreach (string str1 in strtemp)
                    {
                        lt.Add(int.Parse(str1));//加入容器
                    }

                    foreach (int n in lt)
                    {
                        if (n % 2 == 0)
                        {
                            opp++;
                            lt1.Add(n);//加入偶容器
                        }
                        else
                        {
                            od++;
                            lt2.Add(n);//加入寄容器
                        }
                    }

                    Console.Write("偶数个数:{0};奇数个数:{1}\n", opp, od);
                    Console.WriteLine("请输入十个整数:");
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
            }
        }
    }
}
第二种:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 奇偶数
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = new string[10];
            for (int i = 0; i < str.Length; i++)
            {
                Console.Write("第" + i + "个数");
               str[i]= Console.ReadLine();
              
            }
            for (int i = 0; i < str.Length; i++)
            {
                if (Convert.ToInt32(str[i]) % 2 == 0)
                {
                    Console.WriteLine(str[i]);
                }
            }
            Console.ReadLine();
           
        }
    }
}
第三种:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace 练习分拣奇数偶数
{
    class Program
    {
        static void Main(string[] args)
        {
            string msg = "2 7 9 3 6 5 8 4";
            string[] nums = msg.Split(' ');
            //7 9 3 5 2 6 8 4

            //存放奇数
            ArrayList listOdd = new ArrayList();
            //存放偶数
            ArrayList listEven = new ArrayList();
            for (int i = 0; i < nums.Length; i++)
            {
                if (Convert.ToInt32(nums[i]) % 2 != 0)
                {
                    listOdd.Add(nums[i]);
                }
                else
                {
                    listEven.Add(nums[i]);
                }
            }
            listOdd.AddRange(listEven);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < listOdd.Count; i++)
            {
                sb.Append(listOdd[i]+" ");
            }
            Console.WriteLine(sb);
            Console.ReadKey();

        }
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值