java Java如何使用循环在数组中添加int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12295466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:20:23  来源:igfitidea点击:

Java how to add int in an array using loop

java

提问by user1645034

i cant seem to figure out the logic, here's my code

我似乎无法弄清楚逻辑,这是我的代码

class stringrays {
public static void main (String[] args) {

    int[] numrays = {23, 6, 47, 35, 2, 14};
    int i;
    int z;
    int y;

    for (i=1; i < numrays.length; i++) {
        z = numrays[0] + numrays[i];
        System.out.println(z);
    }
}

the above results shows 29 70 58 25 37

以上结果显示 29 70 58 25 37

which means that array 0 adds array 1, then array 0 adds array array 2 and so on.

这意味着数组 0 添加数组 1,然后数组 0 添加数组数组 2,依此类推。

what i want, is to add the first array 0 onto the next array and so on.. using a loop condition.

我想要的是将第一个数组 0 添加到下一个数组上,依此类推..使用循环条件。

then get the average of the sum.

然后得到总和的平均值。

采纳答案by Amit Deshpande

Remove numrays[0]and replace it with z

删除numrays[0]并替换为z

     int z =0;
for (i = 0; i < numrays.length; i++) {
        z = z + numrays[i];
        System.out.println("Sum:"+z);

    }
    System.out.println("Average:"+z/numrays.length);

回答by mssb

Try this,

试试这个,

int[] numrays = {23, 6, 47, 35, 2, 14};
int z = 0;

for (int i=0; i < numrays.length; i++) {
    z = z + numrays[i];
    System.out.println(z);
}
    System.out.println("Average : "+(z/numrays.length) );
}

回答by CloudyMarble

If you mean 23, 6 then 6 + 47 and so on you need to do:

如果您的意思是 23、6 然后是 6 + 47 等等,您需要执行以下操作:

for (i=0; i < numrays.length - 1; i++) 
{
   z = numrays[i] + numrays[i + 1];
   System.out.println(z);
}

回答by Priyank Doshi

Or the LambdaJway :

LambdaJ方式:

int sum = sum(asList(1, 2, 3, 4, 5));

回答by bigGuy

Check your logic. Now you are printing sum of first and n-th number in array; old z value is lost. For sum in loop, use like z = z + something

检查你的逻辑。现在您正在打印数组中第一个和第 n 个数字的总和;旧的 z 值丢失。对于循环中的总和,使用 likez = z + something

回答by Michele Mariotti

it is unclear what you need...

不清楚你需要什么......

to add a value to the next position:

为下一个位置添加一个值:

int[] numrays = {23, 6, 47, 35, 2, 14};

for(int i = 0; i < numrays.length - 1; i++) {
    numrays[i] += numrays[i + 1];
}
System.out.println(Arrays.toString(numrays));

to get the mean value:

得到平均值:

int[] numrays = {23, 6, 47, 35, 2, 14};
double sum = 0;

for(int i = 0; i < numrays.length; i++) {
    sum += numrays[i];
}

double mean = sum / numrays.length;


System.out.println(mean);

回答by The Cat

You could use a "for each" loop, to sum the contents of the array and then find the average.

您可以使用“for each”循环,对数组的内容求和,然后求平均值。

int sum = 0;

for(int each : numrays)
{
   sum = sum + each;
}

float average = each / numrays.length;