ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-12-14 11:00:54  阅读:122  来源: 互联网

标签:Console 编写程序 C# sum System 偶数 int ji using


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

代码:

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有