简体   繁体   English

将嵌套循环的输出存储为新数组 - Java

[英]Storing output of a nested loop as new array - Java

I need to find the largest prime number of a given array when adding two numbers in an array,so I decided to add all possible sums first and displayed it.在数组中添加两个数字时,我需要找到给定数组的最大素数,因此我决定先添加所有可能的总和并显示它。 Now I want to take those output elements to a new array.Please help me solve this problem.现在我想把这些输出元素放到一个新的数组中。请帮我解决这个问题。

import java.util.Scanner;

public class main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int noOfElem = scanner.nextInt();
            int[] array = new int[noOfElem];
            int[][] newArray = new int[5][4];
            int i=0;
            while(scanner.hasNextInt()){
                array[i] = scanner.nextInt();
                i++;
                if(i == noOfElem){
                    break;
                }
            }
            for (int a = 0; a < array.length; a++)
            {
                for (int b = a+1; b < array.length; b++) {
                    int m = array[a] + array[b];
                    newArray[a][b] = 
                }
            }
        } 
}

Not quite sure what the problem is here, just do不太确定这里的问题是什么,只是做

newArray[a][b] = m;

This stores all sums of all 'a's and 'b's such that newArray[a][b] is the sum of array[a] + array[b]这存储了所有 'a's 和 'b's 的所有总和,这样 newArray[a][b] 是 array[a] + array[b] 的总和

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

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