java 任意输入一个数字判断是否为素数应该如何编写啊

[复制链接]
查看11 | 回复1 | 2011-3-29 07:29:54 | 显示全部楼层 |阅读模式
回复

使用道具 举报

千问 | 2011-3-29 07:29:54 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">package Test;

import java.util.*;
public class tests {

/**测试

* @param args

*/

public static void main(String[] args) {

System.out.println(\"请输入一个正整数:\");

int a = (new Scanner(System.in)).nextInt();

if(isPrimeNumber(a) == true){

System.out.println(a \"是素数\");

}

else{

System.out.println(a \"不是素数\");

}

}


public static boolean isPrimeNumber(int number){

boolean flag=true;

if(number=0)

throw new IllegalArgumentException(\"number是不合法的参数!\");

for(int i=2;i=Math.sqrt(number);i){

if(number%i==0)

flag=false;

if(flag==false)

break;

}

return flag;

}
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行