简体   繁体   English

在1个循环中循环2个数组

[英]Looping through 2 Arrays in 1 loop

I have been working on this one loop for too long and cannot figure it out. 我已经在这个循环上工作了太长时间,无法弄明白。 My program has 2 jtextareas named xpointsArea and ypointsArea and i need it to loop through both and get the sumnation of (x * y) for instance if i had 2,4,6 in the xArea and 5,3,2 in the yArea then it would mulitply 2 * 5, 4 * 3, 2 * 6 and then add the products so the answer would be 34. 我的程序有两个名为xpointsArea和ypointsArea的jtextareas,我需要它来遍历两者并获得(x * y)的求和,例如,如果我在xArea中有2,4,6,在yArea中有5,3,2那么它会多次2 * 5,4 * 3,2 * 6然后添加产品,所以答案是34。

Here is what i have: ` 这就是我所拥有的:`

    String[] xpoints = xpointsArea.getText().split("\\n");
    String[] ypoints = ypointsArea.getText().split("\\n");

    for (String xpoint : xpoints) {
        double x = Double.parseDouble(xpoint);
        double y = Double.parseDouble(ypoint);

        sumnationxy += (x * y);
    }`

I cannot figure out how to get the value of the ypoint without making a nested forloop which wouldnt work. 我无法弄清楚如何在不制作嵌套的forloop的情况下获得ypoint的值。 Please help, thanks. 请帮忙,谢谢。

for (int index = 0; index < xpoints.length; index++) {
    double x = Double.parseDouble(xpoints[index]);
    double y = Double.parseDouble(ypoints[index]);

    sumnationxy += (x * y);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM