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

using System;
using System.Collections.Generic;


namespace sy1_4{
    class Program{
        static void Main(string[] args){
            List<string> list = new List<string>();
            int len = 0;//数组长度
            int sum1 = 0;//奇数和
            int sum2 = 0;//偶数和

            Console.WriteLine("请输入数组元素,以‘e’结束");
            while(true) {
                string input = Console.ReadLine();
                if (input.Equals("e") == false) {
                    list.Insert(len++, input);
                } else {
                    break;
                }
            }

            string[][] nums = new string[len][];
            for (int i = 0; i < len; i++) {
                nums[i] = list[i].Split();
            }

            for (int i = 0; i < len; i++) {
                for (int j = 0; j < nums[i].Length; j++) {
                    if (int.Parse(nums[i][j]) % 2 == 1)
                    {
                        sum1 = sum1 + int.Parse(nums[i][j]);
                    } else { 
                        sum2 = sum2 + int.Parse(nums[i][j]);
                    }
                }
            }
            Console.WriteLine("奇数和为{0}",sum1);
            Console.WriteLine("偶数和为{0}",sum2);
        }
    }
}

 

posted @ 2021-09-18 21:14  Arisf  阅读(712)  评论(0编辑  收藏  举报