简体   繁体   中英

Why does my primality test stop after 1 and not seem to be able to go on?

I apologize if this is a dumb question, I'm pretty new to Java, but I can't figure out why my mod operator based primality test for numbers 1-100 stops at 1. I've tried following my code and I don't understand why it fails to continue to the last else statement when possiblePrime = 2.

It should just take possiblePrime = 2 through the if and else if statements all the way to the last else statement and print "2 is a prime." and then continue on to possiblePrime = 3, but instead it goes through the loops as it should when possiblePrime = 1, as it is when it is initialized at the start, and then stops entirely once possiblePrime is incremented at the end of the last else statement, solely printing "1 is a prime."

Thank you for any help you might be able to offer, it is much appreciated! I am definitely racking my brain trying to figure this out, and I'm almost 100% sure it is some stupid and obvious mistake I'm just not seeing.

public class PrimeFind {
    public static void main(String[] args){
        int possiblePrime = 1;
        for(int i = 1 ; i <= 100 ; i++){
            int possibleDivisor = 1;
            if(possiblePrime%possibleDivisor != 0){
                possibleDivisor++;
            }
            else if(possiblePrime != possibleDivisor){
                possiblePrime++;
            }
            else{
                System.out.println(possiblePrime + " is a prime.");
                possiblePrime++;
            }
        }
    }
}

You are setting possibleDivisor = 1 inside the for loop. hence it will always equal 1. This in turn will make the modulo operation equal to 0 in every case. The possiblePrime will always be different from the possibleDivisor (1) except for 1. Hence you only get 1.

public class PrimeFind {
    public static void main(String[] args){
        int possiblePrime = 1;
        for(int i = 1 ; i <= 100 ; i++){
            int possibleDivisor = 1; // Always 1
            if(possiblePrime%possibleDivisor != 0){ // Always False because something%1 == 0
                possibleDivisor++;
            }
            else if(possiblePrime != possibleDivisor){ // Always True except for possiblePrime=1
                possiblePrime++;
            }
            else{
                System.out.println(possiblePrime + " is a prime.");
                possiblePrime++;
            }
        }
    }
}

You had put the wrong conditions that is why this is happening, your first if statement if(possiblePrime%possibleDivisor != 0) is always going to evaluated as false because you initialized possibleDivisor with value 1 and none of the numbers gives value other than 0 from division by 1 so in your code the statement possibleDivisor++; will never going to execute.

And your else if statement else if(possiblePrime != possibleDivisor) every time evaluates as true except i=0 . That is why it won't go to else block and print your statement.

Your for loop doesn't get stop after i=0 but just because of your else block isn't executes, it seems to you as like loop stopped after the first iteration.

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