问题
This code is to print the series of prime number up to given limit but when I am trying to execute this,it goes into infinite loop.
import java.io.*;
class a
{
public static void main(String s[]) throws IOException
{
int count=1;
String st;
System.out.println("how many prime no. do you want");
BufferedReader obj= new BufferedReader (new InputStreamReader (System.in));
st=obj.readLine();
int n=Integer.parseInt(st);
while(count!=n)
{
int num=2;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
count++;
break;
}
}
num++;
}
}
}
回答1:
USe isPrime(int num) method in loop to generate prime numbers example is show below code
public class PrimeNumbersSeries
{
public boolean isPrime(int num)
{
boolean flag=true;
for(int i=2; i<=num/2; i++)
{
if(num%i==0)
{
flag=false;
}
else
{
flag=true;
}
}
if(num<=1)
{
flag=false;
}
return flag;
}
public static void main(String []args)
{
System.out.println("how many prime no. do you want");
PrimeNumbersSeries prime=new PrimeNumbersSeries();
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int count =0;
int j=1;
while(count!=num)
{
if(prime.isPrime(j))
{
System.out.print(j+", ");
count++;
}
j++;
}
}
}
回答2:
Problem is value of num is always 2 at the start of loop,even if you say num++
again it takes num=2
which is start statement and wont enter into for loop ever,hence so infinite loop.This will Work
int num=2;
while(count!=n) {
for(int i=2;i<num;i++) {
if(num%i==0) {
count++;
break;
}
}
num++;
}
回答3:
Here is your working code:
import java.io.*;
class A
{
public static void main(String s[]) throws IOException
{
int count=2;
String st;
System.out.println("how many prime no. do you want");
BufferedReader obj= new BufferedReader (new InputStreamReader (System.in));
st=obj.readLine();
boolean isPrime = false;
int n= Integer.parseInt(st);
int num=3;
if(n>=1){
System.out.println(2);
}
while(count<=n)
{
//No need to go up to num. Up to sqrt(num) will do.
for(int i=2;i<=Math.sqrt(num);i++)
{
if(num%i==0)
{
isPrime = false;
break;
}
}
if(isPrime){
System.out.println(num);// Added the print
count++;
}
isPrime = true;
num++;
}
}
}
回答4:
After correction i got the series of prime numbers.
import java.io.*;
class a
{
public static void main(String s[]) throws IOException
{
int count=1,count1=0;
String st;
System.out.println("how many prime no. do you want");
BufferedReader obj= new BufferedReader (new InputStreamReader (System.in));
st=obj.readLine();
int n=Integer.parseInt(st);
int num=2;
while(count<=n)
{
count1=0;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
count1++;
}
}
if(count1==0)
{
System.out.println(num);
count++;
}
num++;
}
}
}
回答5:
You can take a look at this code.
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
//How many prime numbers you want to print?
String s = bf.readLine();
int n = Integer.parseInt(s);
int count = 0;
boolean loop = true;
for(int i =2 ; loop ; i++){
if(isPrime(i))
{
System.out.println(i + " ");
count++;
}
if(count == n)
loop = false;
}
}
The following isPrime() method checks whether a number is prime or not. If the number is prime, it returns true, otherwise false.
public static boolean isPrime(int num) {
boolean prime = true;
for(int i=2 ; i<= Math.sqrt(num);){
if(num % i == 0)
{
prime = false;
break;
}
if(i >= 3)
/* when i>=3 we do not need to check for every number.
For avoiding even numbers i is incremented by 2.
It reduces the number of looping */
i+=2;
else
i++;
}
return prime;
}
}
回答6:
import java.util.*;
class Prime
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the no of prime nos want: ");
int n2 = sc.nextInt();
int flag = 1 ;
int count = 0 ;
for (int i =2; i<99999;i++ )
{
for (int j=2; j<i;j++ )
{
if (i%j == 0)
{
flag = 0;
break;
}
else
{
flag =1;
}
}
if (flag == 1)
{
System.out.print(i +"\t");
count++ ;
}
if (count == n2)
{
break ;
}
}
}
}
来源:https://stackoverflow.com/questions/32664939/program-to-print-series-of-prime-numbers-using-java