每日博客

C#

编写程序,计算数组中奇数之和和偶数之和。

namespace test

{

class test_1

    {

public void test_1_4()

        {

            int t1=0,t2=0;

            int[] arr=new int[]{1,2,3,4,5,6,7,8,9,10};

            foreach(int x in arr)

            {

                if(x%2==0){t1+=x;}

                else t2+=x;

            }

            Console.WriteLine("数组中偶数之和为:{0}",t1);

            Console.WriteLine("数组中奇数之和为:{0}",t2);

        }

    }

    class main

    {

        static void Main(string[] args)

        {

            test_1 t = new test_1();

            t.test_1_4();

        }

    }

}

 

posted @ 2021-10-05 20:04  谦寻  阅读(34)  评论(0编辑  收藏  举报