简体   繁体   中英

How do I assign array values with a while loop ?

I'm trying to make a while loop that iterates through every long number possible and add every prime number it encounters into an the primes array. Since the while loop is supposed to run until the length of primes is 200, I expect the primes array to be filled with the first 200 prime numbers. Instead I get all zeroes. I have successfully gotten 20 rows of 10 characters each with a space in between them. How may I get them to be the actual prime numbers though?

public class PrimeGenerator {

    public static void main(String[] args) {

        long primes[] = new long[200]; 

        while (primes.length > 200){

            for(long y  = 2; y < Long.MAX_VALUE; y++) {
                int primeCounter = 0;

                if (isPrime(y) == true){
                    primes[primeCounter] = y;
                    primeCounter++;
                }
            }


        }

        for (int i = 0; i < 20; i++) {

            int primeCounter = 0;

            for(int p = 0; p < 10; p++) {
                System.out.print(primes[primeCounter] + " ");
                primeCounter++;
            }

            System.out.println();
        }
    }


    public static boolean isPrime(long number) {

        if (number % 2 == 0)
            return false;
        if (number == 2)
            return true;

        for(int x = 3; x*x <= number; x+=2) {   
            if (number%x == 0)
                return false;   

        } 

        return true;


    }

}

primes.length is always 200 so the while loop is never entered.

The while loop is useless. Just add a condition to the for loop that would exit when the entire array has been assigned. Also move the initialization of primeCounter to be outside the for loop. Otherwise all the primes will be assigned to primes[0] .

long primes[] = new long[200]; 
int primeCounter = 0;
for(long y  = 2; y < Long.MAX_VALUE && primeCounter < 200; y++) {
    if (isPrime(y) == true){
        primes[primeCounter] = y;
        primeCounter++;
    }
}
for (int i = 0; i < primes.length; i++) {
    System.out.print(primes[i]);
    if ((i+1) % 10 == 0)
        System.out.println();
}

EDIT :

As Sweeper commented, you should also fix your isPrime method, since it returns false for 2 :

public static boolean isPrime(long number) {
    if (number == 2)
        return true;
    if (number % 2 == 0)
        return false;

    for(int x = 3; x*x <= number; x+=2) {   
        if (number%x == 0)
            return false;   
    } 
    return true;
}

this code down

    long primes[] = new long[200]; 

    while (primes.length > 200){

means

while (200 > 200){

or the same as

while (false){

so your loop is NEVER executed!

because you did:

while (primes.length > 200)

and the length of the array is always 200,you never get into the while loop , and the zero in the array are coming because when you create array of "long" it initialized him with zeros

Firstly, the length of an array doesn't change. So, when you are testing for primes.length > 200 this will always be false, and the loop is never even entered. Therefore all values in the array are left at the default value of 0 .

For doing this I would doing something like the following:

int primeCounter = 0;
long current = 0L;
while(primeCounter < primes.length){
    if(isPrime(current)){
        primes[primeCounter] = current;
        primeCounter++;
    }
    current++;
}

An array's length never changes. If you declared an array to have a length of 200, it will always have a length of 200. Because of this, your while loop is never executed, not even once.

There are a lot of other errors in the code, so I tried to create a solution with as few changes as possible:

public static void main(String[] args) {

    int primeCounter = 0;
    long nextPossiblePrime = 2;
    long primes[] = new long[200];

    while (primeCounter < 200) {

        for (long y = nextPossiblePrime; y < Long.MAX_VALUE; y++) {

            if (isPrime(y) == true) {
                primes[primeCounter] = y;
                primeCounter++;
                nextPossiblePrime = y + 1;
                break;
            }
        }


    }

    primeCounter = 0;
    for (int i = 0; i < 20; i++) {

        for (int p = 0; p < 10; p++) {
            System.out.print(primes[primeCounter] + " ");
            primeCounter++;
        }

        System.out.println();
    }
}


public static boolean isPrime(long number) {
    if (number == 2 || number == 3)
        return true;

    if (number % 2 == 0)
        return false;


    for (int x = 3; x * x <= number; x += 2) {
        if (number % x == 0)
            return false;

    }

    return true;


}

The first problem is that you created two primeCounter s, which is not needed. I removed the extra one and moved the scope of it to the scope of the method. The next problem is that your first for loop doesn't remember the prime number that it is on and it doesn't stop when it has found one so it will keep adding the 200th prime to the array. I fixed this by adding a nextPossiblePrime variable that stores what number should the program check next. The last problem is that your isPrime method is written incorrectly. I fixed it for you!

Here's another (cleaner) solution, which still uses a while loop:

public static void main(String[] args) {
    ArrayList<Long> primes = new ArrayList<>();
    long y = 2;
    while (y < Long.MAX_VALUE && primes.size() < 200) {
        if (isPrime(y) == true){
            primes.add(y);
        }
        y++;
    }
    for (int i = 0; i < primes.size(); i++) {
        System.out.print(primes.get(i) + " ");
        if ((i+1) % 10 == 0)
            System.out.println();
    }
}


public static boolean isPrime(long number) {
    if (number == 2 || number == 3)
        return true;

    if (number % 2 == 0)
        return false;


    for (int x = 3; x * x <= number; x += 2) {
        if (number % x == 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