
C#:完整程序代码:求一维数组中各元素的偶数之和
1个回答
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 一维数组中各元素偶数之和
{
class Program
{
static void Main(string[] args)
{
int total=0;//total用来记录arr数组元素的偶数的和
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 23, 24, 56 };//静态初始化一维数组
foreach(int x in arr)//从数组arr中提取整数
{
if (x % 2 == 0)
total += x;
}
Console.WriteLine("数组中偶数之和为:{0}",total);
}
}
}
测试过,正确,至于细节,自行改变
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 一维数组中各元素偶数之和
{
class Program
{
static void Main(string[] args)
{
int total=0;//total用来记录arr数组元素的偶数的和
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 23, 24, 56 };//静态初始化一维数组
foreach(int x in arr)//从数组arr中提取整数
{
if (x % 2 == 0)
total += x;
}
Console.WriteLine("数组中偶数之和为:{0}",total);
}
}
}
测试过,正确,至于细节,自行改变
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询