判断是不是质数()

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        boolean isPrime = true;
        for (int i = 2; i < n; i ++){
            if (n % i == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime && n != 1)
            System.out.println("yes");
        else 
            System.out.println("no");
    }
}

参考

  • https://www.acwing.com/file_system/file/content/whole/index/content/6694185/
————————
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        boolean isPrime = true;
        for (int i = 2; i < n; i ++){
            if (n % i == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime && n != 1)
            System.out.println("yes");
        else 
            System.out.println("no");
    }
}

参考

  • https://www.acwing.com/file_system/file/content/whole/index/content/6694185/