查找并返回可被2或3整除的数组所有元素的总和

时间:2018-10-13 01:24:13

标签: java

我是编程新手,每当我运行此代码时,都会遇到运行时错误[NZEC]。我不知道如何解决该错误

export class Endereco {
    cep : string;
    rua : string;
    complemento: string;
}

1 个答案:

答案 0 :(得分:0)

将您的findSum更改为以下内容。您只需要1个循环

public static int findSum() {
    Scanner s = new Scanner(System.in);
    System.out.println("Enter total input ");
    int n = s.nextInt();
    int arr1 [] = new int[n];
    int sum = 0;

    System.out.println("Enter the " + n + " inputs ");
    for (int i = 0; i < n; i++) {
        arr[i] = s.nextInt();
        if (arr[i] % 2 == 0 || arr[i] % 3 == 0) {
            sum = sum + arr[i];
        }
    }

    return sum;
}