[英]I am trying to find the prime numbers from 0 to a given number from the user
[英]In Java, I am trying for find a prime number between the range form 10 to 99, but I received that all the numbers in the range are not prime number
我已經嘗試了該程序,但我出錯了 output 請幫我解決我遇到的問題
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("Enter the Starting Number : ");
int snum=obj.nextInt();
System.out.println("Enter the Ending Number : ");
int endnum=obj.nextInt();
Boolean isprime=true;
for(int i=snum;i<endnum;i++)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
{
isprime=false;
break;
}
}
if(isprime)
{
System.out.println(i+" is PRIME NUMBER");
}
else
{
System.out.println(i+" is NOT PRIME NUMBER");
}
}
}
}
您需要在循環中每次重新初始化 isprime:
for(int i=snum;i<endnum;i++)
{
isprime = true;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
isprime=false;
break;
}
}
if(isprime)
{
System.out.println(i+" is PRIME NUMBER");
}
else
{
System.out.println(i+" is NOT PRIME NUMBER");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.