Method to calculate prime numbers - [JAVA] -
i need build method calculates prime number homework. implemented algorithm proposed professor it's not working.
eclipse gives message: the local variable prime may not have been initialized
, not compile.
can me?
public static boolean itsprime(int nbtest){ boolean prime; if (nbtest <= 1){ prime = false; } else if (nbtest == 2){ // 2 number prime prime = true; } else if ((nbtest != 2) && (nbtest % 2 == 0)){ // if it's , different 2 not prime prime = false; } else { for(int = 3; <= math.sqrt(nbtest); = i+2){ if (nbtest % == 0){ prime = false; } else { prime = true; } } } return prime; }
just initialise when declare variable. eg
boolean prime = false;
Comments
Post a Comment