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();
        }
    }
}

运行结果:

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

上一篇:李渔《笠翁对韵 》


下一篇:管道符、重定向与环境变量