java编程求出2~N之间的所有素数..N由键盘输入. (麻烦给个代码)
java编程求出2~N之间的所有素数..N由键盘输入. (麻烦给个代码)
package number11;
import
import
import
public class Num7 {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
int j=0;int m=2,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入数值n:");
n= Integer.parseInt(br.readLine());
for(m=2;m<=n;m++)
{
boolean flag=true;//必须放在这里,每次循环初始化
for(int i=2;i<=Math.sqrt(m);i++)
{
if(m%i==0)
{
flag=false;
break;
}
}
if(flag)
{
j++;System.out.println(" "+m);}
}
System.out.println("共"+j+"个");
}
}