简体   繁体   中英

Prime number required by user in Java

I want to display the prime number required by the user. For example, if the user wants 3rd prime, I will display 5. I have the following java code.

import java.util.Scanner;

public class Prime {

private static Scanner scanner;


public static void main(String args[]) {

 //get input till which prime number to be printed
 // System.out.println("Enter which prime number to be printed: ");
//  scanner = new Scanner(System.in);
//  int limit = scanner.nextInt();
  int count = 0;
  int number = 2;
  //System.out.println("Printing prime number from 1 to " + limit);
  while(count<=3)
  {
      if(isPrime(number)){
          count++;
         // System.out.println(count);
      }
      number++;
  }
  if(count == 3)
      System.out.println("10001 prime is "+number);

    }


public static boolean isPrime(int number){
    for(int i=2; i<number; i++){
       if(number%i == 0){
           return false; 
       }
    }
    return true; 
}
}

When i run it, I am not able to gett any output. Where am i going wrong? PS: For time being, I am running the loop only until 3.

You have while(count <= 3) so when you exit the loop, count == 4 .

Therefore, your if(count == 3) is never entered and nothing is printed.

Anyways the better solution would be

public void calcPrime(int inp) {
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(2);
arr.add(3);

int counter = 4;

while(arr.size() < inp) {
    if(counter % 2 != 0 && counter%3 != 0) {
        int temp = 4;
        while(temp*temp <= counter) {
            if(counter % temp == 0)
                break;
            temp ++;
        }
        if(temp*temp > counter) {
            arr.add(counter);
        }
    }
    counter++;
}

System.out.println("finish" +arr.get(inp-1));
}
}

Here is corrected code that works.

public class Main {

public static void main(String args[]) {
    //Returns fourth prime number
    System.out.println(getPrimeNumber(4));


}

public static int getPrimeNumber(int order) {

    int currentOrder = 1;
    int currentNumber = 1;

    while (currentOrder < order) {
        currentNumber++;
        if (isPrime(currentNumber)) currentOrder++;
    }

    return currentNumber;

}


public static boolean isPrime(int number) {
    for (int i = 2; i < number; i++) {
        if (number % i == 0) {
            return false;
        }
    }
    return true;
}

}

There was no need to start your counter at 0 and it mathematically wrong to start with number 2 ! Just begin with order and currentNumber initialized at 1 and if first prime number is what your user is looking for, no loop will be required !

Also, your loop condition after correct variable initialization was corrected from "<=" to "<".

That's all !

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