数组创建和添加 [英] Array creation and addition

查看:58
本文介绍了数组创建和添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有这种格式的数组int [] i = {0,1,2,3,4,5,6,7,8,9};

请帮我一个可以用来获取数组中值总和的过程.

例如.

Hi there,

I have an array in this format int[] i = {0,1,2,3,4,5,6,7,8,9};

Please help me with a procedure thatI can use to get the sum of the values in my array.

Eg.

int value=(0+1+2+3+4+5+6+7+8+9);
int sum= value;



在此先感谢

Martin



Thanks in advance

Martin

推荐答案

有很多方法,您可以这样做.

there are many ways, you can do like this.

int sum = i.Sum();


尝试下面的代码.

Try below code.

int[] i = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int sum = i.Sum();


通常要做的是创建一个int来保存总值并将其设置为0.
您对数组中的每个值进行for循环,将其添加到总计中.循环完成后,您便拥有了总数.
The usual thing to do is to create an int to hold the total value and set it to 0.
The you do a for loop across each value in the array adding it to the total. Once the loop is done you have your total.


这篇关于数组创建和添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆