Java教程

Java质数操作

Java中的质数是一个大于1且除以1或仅被自身除的数字。换句话说,质数不能除以自身或1以外的其他数。例如,2、3、5、7、11、13、17 ...是质数。
注意: 0和1不是质数。 2是唯一的偶数质数,因为所有其他偶数都可以除以2。
让我们看看Java中的质数程序。在此Java程序中,我们将使用数字变量,并检查数字是否为质数。
public class PrimeExample{
    public static void main(String args[]){
        int i,m=0,flag=0;
        int n=3;
        //it is the number to be checked m=n/2;
        if(n==0||n==1){
            System.out.println(n+"不是质数");
        }
        else{
            for(i=2;i<=m;i++){
                if(n%i==0){
                    System.out.println(n+" 不是质数");
                    flag=1;
                    break;
                }
            }
            if(flag==0) {
                System.out.println(n+" 是质数");
            }
        }
    }
}
输出:
3 是质数

使用Java中的方法的质数程序

public class PrimeExample2{
    static void checkPrime(int n){
        int i,m=0,flag=0;
        m=n/2;
        if(n==0||n==1){
            System.out.println(n+" 不是质数");
        }
        else{
            for(i=2;i<=m;i++){
                if(n%i==0){
                    System.out.println(n+" 不是质数");
                    flag=1;
                    break;
                }
            }
            if(flag==0) {
                System.out.println(n+" 是质数");
            }
        }
    //end of else}
    public static void main(String args[]){
        checkPrime(1);
        checkPrime(3);
        checkPrime(17);
        checkPrime(20);
    }
}
输出:
1 不是质数
3 是质数
17 是质数
20 不是质数

Java中的常用质数程序(另一种方式)

您还可以使用未预定义编号的方法。在这里,用户必须输入数字以检查数字是否为质数。
import java.util.Scanner;
import java.util.Scanner;
public class PrimeExample3 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int n = s.nextInt();
        if (isPrime(n)) {
            System.out.println(n + " 是质数");
        }
        else {
            System.out.println(n + " 不是质数");
        }
    }
    public static boolean isPrime(int n) {
        if (n <= 1) {
            return false;
        }
        for (int i = 2;i <Math.sqrt(n);i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
}

在两个数字之间查找质数

您还可以在两个指定的数字之间查找质数数字。
import java.util.Scanner;
public class PrimeExample4 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the first number : ");
        int start = s.nextInt();
        System.out.print("Enter the second number : ");
        int end = s.nextInt();
        System.out.println("List of 质数s between " + start + " and " + end);
        for (int i = start;i <= end;i++) {
            if (isPrime(i)) {
                System.out.println(i);
            }
        }
    }
    public static boolean isPrime(int n) {
        if (n <= 1) {
            return false;
        }
        for (int i = 2;i <= Math.sqrt(n);i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
}
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4