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


using System;
using System.Collections.Generic;

namespace Test_1_4
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> str = new List<string>();
            int len = 0;
            int oddsum = 0;
            int evensum = 0;
            Console.WriteLine("请输入数组:");
            while (true)
            {
                string input = Console.ReadLine();
                if (input.Equals("") == false)//如果不是输入为空则增加输入记录
                    str.Insert(len++, input);
                else
                    break;

            }
            string[][] every = new string[len][];//交叉数组,行固定,为上面得到的行数,每一行的长度不定(每行字符间以空格或其他分割)
            for(int i = 0; i < len; i++)
            {
                every[i] = str[i].Split();// C#对空格的分割方式之一
            }
            for(int i = 0; i < len; i++)
            {
                for(int j = 0; j < every[i].Length; j++)
                {
                    int aa;
                    aa = int.Parse(every[i][j]);
                    if ((aa % 2) == 1)
                    {
                        oddsum += aa;
                    }
                    else
                    {
                        evensum += aa;
                    }
                }
            }
            Console.WriteLine("奇数之和等于:");
            Console.WriteLine(oddsum);
            Console.WriteLine("偶数之和等于:");
            Console.WriteLine(evensum);
            Console.ReadKey();
        }
    }
}

截图

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM