简体   繁体   中英

PRIME1 - Prime Generator Java

I wrote the code in JAVA and it seems to work fine in Eclipse. But I get an error when I submite the code in SPOJ(runtime error (NZEC) ).

Could someone please help me with this.

Here what I tried so far:

class Main {
    public static void main(String[] args) throws java.lang.Exception {


        Scanner n1 = new Scanner(System.in);
        Scanner n3 = new Scanner(System.in);
        int n2 = n1.nextInt();
        int[][] n4 = new int[n2][2];

        for (int i = 0; i < n2; i++) {
            String[] s2 = n3.nextLine().split(" ");
            for (int j = 0; j < 2; j++) {
                n4[i][j] = Integer.parseInt(s2[j]);
                }
            }   
        for (int i = 0; i < n2; i++){
            for(int j=n4[i][0];j<=n4[i][1];j++){
                if(isPrimeNumber(j)){
                    System.out.println(j);
                    }

                }
            System.out.println();
            }

        }



    public static boolean isPrimeNumber(int number) {
        if (number == 2 || number == 3) {
            return true;
            }
        if (number % 2 == 0) {
            return false;
            }
        int sqrt = (int) Math.sqrt(number) + 1;
        for (int i = 3; i < sqrt; i += 2) {
            if (number % i == 0) {
        return false;
        }
            }
        return true;
        }

    }

1st thing you need to do is use the scanner properly and get rid off the 2nd one..

you are using 2 scanners because only one is not working as expected, why? because you forgot that the Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine

Try this:

public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        int n2 = input.nextInt();
        input.nextLine();
        int[][] n4 = new int[n2][2];

        for (int i = 0; i < n2; i++) {
            String string2 = input.nextLine();
            String[] s2 = string2.split(" ");
            for (int j = 0; j < 2; j++) {
                n4[i][j] = Integer.parseInt(s2[j]);
            }
        }
        for (int i = 0; i < n2; i++) {
            for (int j = n4[i][0]; j <= n4[i][1]; j++) {
                if (isPrimeNumber(j)) {
                    System.out.println(j);
                }

            }
            System.out.println();
        }

    }


    public static boolean isPrimeNumber(int number) {
        if (number == 2 || number == 3) {
            return true;
        }
        if (number % 2 == 0) {
            return false;
        }
        int sqrt = (int) Math.sqrt(number) + 1;
        for (int i = 3; i < sqrt; i += 2) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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