You can go even further and make userNumber an argument of getPrime: public boolean getPrime(int userNumber) Int numRoot = ((int) Math.sqrt(userNumber))
More precisely, factors should be reset to 0 at the start of getPrime.Ī better way to go about this is by making x, numRoot, isPrime and factors local variables of getPrime: public boolean getPrime() The problem is that you're using too many instance variables inside getPrime, causing you to unintentionally inherit state from previous iterations. ('\nThe prime numbers from 1 to %d are: \n 2', end) When I use this method and a while loop to get a list of prime numbers from 1 to a large number it keeps returning 24 then 4 then 5.the variable end, in code below is asked for in a prime class runner separately. I have a getPrime method which returns true if the number is prime or false if it is not prime. I need to create a program that can get all the prime numbers from 1 to a large number.