简体   繁体   中英

listing prime numbers from user input in java

in java what i am trying to do is have a user input a value greater than 0 and with that number they input list that amount of prime numbers starting from 2

so if the user inputs "3" the program will display 2,3,5 if the user inputs "5" the program will display 2,3,5,7,11 and so on

the problem is I cant figure out how to have the user input do this correctly, i either end up with the numbers repeating however many times or the list ending at the user input, any help would be apreciated

import java.util.Scanner;

public class Primes 
{

    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);


        int n = console.nextInt();

        if(n<=0)
        {
            return;
        }
        else
        {
            for(int i=2; i < 100; i++)
            {
                boolean isPrime = true;

                for(int j=2; j < i; j++)
                {
                    if(i%j == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if(isPrime)
                {
                    System.out.println(i);
                }
            }
        }

    }

}

I rather have code that is refactored, each method is doing one thing, it makes it much easier to read, debug and maintain.

All we need to do is separate the logic that checks if a number is prime from the logic that goes over the numbers until n prime numbers are found:

public static void main(String[] args) {
    printNPrimes(5);
}

static private boolean isPrime(int n) {
    for (int i = 2; i <= Math.sqrt(n); i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

static private void printNPrimes(int n) {
    int i = 2;
    while (n > 0) {
        if (isPrime(i)) {
            System.out.println(i + " is Prime");
            n--;
        }
        i++;
    }
}

Keep a count of how many primes have been found by changing your for loop to stop when you've found enough primes and performing primesFound++ when a prime is found:



    
    {
        boolean isPrime = true;

        for (int j = 2; j < i; j++)
        {
            if (i % j == 0)
            {
                isPrime = false;
                break;
            }
       }
       if (isPrime)
       {
           System.out.println(i);
           
       }
    }
    
//prime 

int i,j;

Set<Integer> primeNums = new HashSet<>();
Set<Integer> notPrimeNums = new HashSet<>();
Stack<Integer> stack = new Stack<>();

for(i=1; i<fiboList.size(); i++) {
    for(j=i+1; j<fiboList.size(); j++) {
        if( i % j == 0 ) {
            notPrimeNums.add(fiboList.get(i));
        }else {
            primeNums.add(fiboList.get(i));
        }
    }
}

stack.addAll(primeNums);
Collections.sort(stack);
System.out.println("Prime numbers:"+" "+stack);

}

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