当前位置:首页 » 编程语言 » 质数java

质数java

发布时间: 2023-03-12 03:43:06

java 判断质数的方法

public static void main(String[] args) {
int count=0;
int prime=1;
while(count<100){
while(true){
prime++;
if(isPrime(prime)){
System.out.print(prime+"||");
count++;
if(count%10==0){
System.out.println(" ");
break;
}
}
}

}
}
public static boolean isPrime(int n){
for(int i=2;i<n;i++){
if(n%i==0){
return false;
}
}
if(n==1){
return false;
}
return true;
}

Ⅱ 用java编程实现判断一个整数是否为质数

素数又称质数:除1和其自身之外,没有其它约数的正整数
2是最小的质数,也是唯一的偶质数
1和0既不是质数又不是合数
合数
public static void prime(int num) {// 能求无限大的质数//但如果所求的范围太大,计算的时间需要很久
int n, m, i = 0;
label1: for (n = 2; n <= num; n++) {
for (m = 2; m <= n / 2; m++) {
if (n % m == 0)
continue label1;
}
i++;
System.out.println("第" + i + "个素数是:" + n);
}
}

Ⅲ java 如何输出1到100间的质数

参考代码如下:

package test;

public class Test {

public static void main(String[] args) {

int j;

for (int i = 2; i <= 100; i++) // 1不是素数,所以直接从2开始循环

{

j = 2;

while (i % j != 0)

j++; // 测试2至i的数字是否能被i整除,如不能就自加

if (j == i) // 当有被整除的数字时,判断它是不是自身

System.out.println(i); // 如果是就打印出数字

}

}

}

(3)质数java扩展阅读:

质数又称素数。一个大于1的自然数,除了1和它自身外,不能整除其他自然数的数叫做质数;否则称为合数。

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

Ⅳ java判断质数

不知道你要的是不是这种效果,程序在你的基础上做了一些修改,在你的基础上增加了假如输入的不是一个数时做什么处理,输入“back”时返回,你可以拿去把程序修改的更完善。。你说的方法都可以调用,在 main方法里面需要new一个对象来调用,除非你把方法定义为静态方法(static)。。
import java.util.Scanner;

public class Prime_Decide {

public void isPrime(String st) { // 判断int整形a是否是素数
try {
if (st.equals("back")) {
Display();
} else {
int a, i;
a = Integer.parseInt(st);
for (i = 2; i <= a / 2; i++) {
if (a % i == 0) {
break;
}
}
if (a == 0) {
System.out.println(a + " 你输入的数不是素数");
} else if (i > a / 2) {
System.out.println(a + " 你输入的数是素数");
} else {
System.out.println(a + " 你输入的数不是素数");
}
Input1();
}
} catch (NumberFormatException e1) {
System.out.println("请你输入正确的数字!!!");
Input1();
}
}

public void isPrime_2(String s1, String s2) {
try {
if (s1.equals("back")) {
Display();
} else {
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
if (a < 0 && a == b && a < b) {
System.out.println("你的输入有误,重新输入:");
}
int j;
for (int i = a; i <= b; i++) {
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
break;
}
}
if (j > i / 2) {
System.out.println(" " + i + " 是素数");
}
}
Input2();
}
} catch (NumberFormatException e) {
System.out.println("请你输入正确的数字!!");
Input2();
}
}

public void Input1() {
System.out.println("请输入你要进行判断的数:");
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
isPrime(st);
}

public void Input2() {
// int a, b;
System.out.println("请输入上界 a= ");
Scanner sc = new Scanner(System.in);
// a = Integer.parseInt(sc.nextLine());
System.out.println("请输入下届 b=");
Scanner st = new Scanner(System.in);
// b = Integer.parseInt(st.nextLine());
isPrime_2(sc.nextLine(), st.nextLine());
}

public void Display() {
System.out.println("请输入你要进行的数的判断:\n1: 单个数的判断\n2: 一段整数范围的判断\n3: 退出");
try {
Scanner sc = new Scanner(System.in);
int c = Integer.parseInt(sc.nextLine());
if (c == 1) {
Input1();
} else if (c == 2) {
Input2();
} else if (c == 3) {
System.exit(1);
} else if (c != 1 & c != 2 & c != 3) {
System.out.println("你输入如的信息有误,请重新输入1、2、3!");
Display();
}
} catch (NumberFormatException e) {
System.out.println("你输入如的信息有误,请重新输入1、2、3!");
Display();
}
}

public static void main(String[] args) {
Prime_Decide pr = new Prime_Decide();
pr.Display();
}
}

希望对你有帮助!!

Ⅳ JAVA 编程方法解决“ 输入一个数判断它是否是质数”

else
if(num1%2!=0&&(num1+1)%2==0){
System.out.println(num1+"是质数");
}
else{
System.out.println(num1+"不是质数");
15%2=1,
(15+1)%2=0
->
15是质数???
修改:
//前面略
else
{
int
flag=1;
for(int
j=2;
j*j<=num1;
j++)//这是质数的判断方法,只要除到这个数的开根号的数为止即可
if
(num1%j==0){System.out.println(num1+"不是质数");
flag=0;
break;}
if(flag)
System.out.println(num1+"是质数");
}
//后面略

Ⅵ 用 java 怎么判断一个数是否为质数

质数:

public static boolean isPrime(int N){if( N < 2 ) return false;

for( int i = 2 ; i*i <= N; i++){if( N % i == 0) return false;return true;}

热点内容
数据库逻辑存储结构 发布:2025-07-10 09:26:56 浏览:917
密码编译找规律 发布:2025-07-10 09:18:10 浏览:511
电影视频缓存后 发布:2025-07-10 09:16:48 浏览:893
服务器搭建需要哪些东西 发布:2025-07-10 09:15:23 浏览:801
无限密码怎么改 发布:2025-07-10 09:14:32 浏览:104
coc按键精灵脚本 发布:2025-07-10 09:12:40 浏览:312
excel表格ftp函数 发布:2025-07-10 09:05:50 浏览:276
u2game的解压密码 发布:2025-07-10 09:05:14 浏览:597
c语言编译器ide苹果下载 发布:2025-07-10 09:05:13 浏览:294
andftp端口 发布:2025-07-10 08:57:04 浏览:607