Error message in the return statement.

I am writing one method having a prototype int. But the method shows an error in the editor, saying that Add a return statement , where the return statement is already present. When I add another return, it works fine. I am writing in an eclipse.

Here is my code:

private static int nextPrime(int n) {
        if(n % 2 == 0)
            n++;
        for(; !isPrime(n); n+=2)

        return n;
        return n;
    }

What is wrong here. Thanks for the help.

+4
source share
2 answers

I think the problem is that your cycle fordoes not have a body. Try to give it one:

private static int nextPrime(int n) {
    if(n % 2 == 0)
        n++;
    for(; !isPrime(n); n+=2) { }

    return n;
}

But actually I think the loop foris the wrong type of loop for this. You can use a loop instead while:

private static int nextPrime(int n) {
    if (n % 2 == 0)
        n++;

    while (!isPrime(n)) {
        n += 2;
    }

    return n;
}
+3
source

return , , , , , ...

, ...

+3

Source: https://habr.com/ru/post/1677416/


All Articles