import java.io.*;
public class Flay
{
//判断一个数,是否为素数(只能被1和它本身整除的数叫素数)
public static void main(String args[])throws IOException
{
int a,i;
bealoon p;
BufferedReader buf;
String str;
buf=new BufferedReader(new InputStreamReader(System.in));
System.out.print("INPUT A INTEGER:")
str=buf.readLine();
a=Integer.parseInt(str);
p=true;
for(i=2;i<a;i++)
if(a%i==0)
p=false;
if(p=true)
System.out.print("a is a Prime");
else
System.out.print("a is not a Prime");
}}我想问的是如果我在FOR循环后面加一个break;语句,
for(i=2;i<a;i++)
{if(a%i==0)
p=false;
break;
}
这两句有什么区别呢,哪个程序才是正确的计算素数的程序呢?
public class Flay
{
//判断一个数,是否为素数(只能被1和它本身整除的数叫素数)
public static void main(String args[])throws IOException
{
int a,i;
bealoon p;
BufferedReader buf;
String str;
buf=new BufferedReader(new InputStreamReader(System.in));
System.out.print("INPUT A INTEGER:")
str=buf.readLine();
a=Integer.parseInt(str);
p=true;
for(i=2;i<a;i++)
if(a%i==0)
p=false;
if(p=true)
System.out.print("a is a Prime");
else
System.out.print("a is not a Prime");
}}我想问的是如果我在FOR循环后面加一个break;语句,
for(i=2;i<a;i++)
{if(a%i==0)
p=false;
break;
}
这两句有什么区别呢,哪个程序才是正确的计算素数的程序呢?
for(int i=2; i<(int)Math.sqrt(a); i++){
if(a%i == 0) {
p = false;
break;
}
谢谢指点哈 3Q