Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

NZEC runtime error in Java

This is my code. Please check it where it is giving runtime exception. I wrote a piece of code to solve this problem.

I keep getting NZEC(runtime error), but I can't find which part of the code can cause any Exception since it only involves simple arithmetic computation (there should be no chance of divided by zero).

The logic of the code doesn't matter, and I just wonder where the exception could be hiding.

Any one can spot any bug? Thanks.

import java.io.BufferedReader;
import java.io.File; // headers
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;
import java.math.BigInteger;

public class Mkequal // class
{
    public static void main(String[] args) throws IOException // main class
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System. in));
        Scanner sc = new Scanner(System. in );
        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) // while loop
        {
            int sum = 0;
            int n = Integer.parseInt(br.readLine()); //number of elements in array
            int arr[] = new int[n];
            for (int i = 0; i < n; i++)
                arr[i] = sc.nextInt();
            for (int i = 0; i < n; i++)
                sum += arr[i];
            if (sum % n == 0) //if divisible by n,print n
                System.out.println(n);
            else
                System.out.println(n - 1);

        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are most probably creating too many memory in program. you must create array(arr) outside the while loop and manipulate it inside the loop..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...