编程语言
首页 > 编程语言> > C#编写程序,计算数组中奇数之和和偶数之和

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

作者:互联网

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

代码:

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

namespace Lab04
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            int count_ji = 0;
            int count_ou = 0;
            int sum_ji = 0;
            int sum_ou = 0;
            Console.WriteLine("请输入数组的大小:");
            n = int.Parse(Console.ReadLine());
            int[] a=new int[n];
            Console.WriteLine("请输入{0}个数字的数组:",n);
            for (int i = 0; i < n; i++) {
                a[i] = int.Parse(Console.ReadLine());
            }
         
            for (int i = 0; i < n; i++)
            {
                if (a[i] % 2 == 0) {
                    sum_ou += a[i];
                    count_ou++;
                }
                if (a[i] % 2 == 1) {
                    sum_ji += a[i];
                    count_ji++;
                }
            }
            Console.WriteLine("数组奇数之和{0},偶数之和{1}", sum_ji,sum_ou);
            Console.Write("\n请按任意键继续...");
            Console.ReadKey();
        }
    }
}

运行结果:

标签:Console,编写程序,C#,sum,System,偶数,int,ji,using
来源: https://www.cnblogs.com/yuxuan-light-of-Taihu-Lake/p/15686824.html