法一:
import java.util.Scanner;public class SuShu {public static void main(String[] args) {Scanner scan = new Scanner(System.in);int number = scan.nextInt();isPrime(number);}public static void isPrime(int number) {int count = 0;for (int i = 2; i < number; i++) {if ((number % i) == 0) {count++;}}if (count == 0) {System.out.println(number + "是素数");} else {System.out.println(number + "不是素数");}}
}
法二:
public static void main(String[] args) {int i = 0;Scanner scan = new Scanner(System.in);int number = scan.nextInt();for (i = 2; i < number; i++) {if (number % i == 0) {System.out.println("不是素数");break;}}if (i >= number) {System.out.println("是素数");}}
法三:
public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int i;for (i = 2;i <= n/2;i++) {if(n%i == 0) {//System.out.println("n不是素数:"+n);break;}}if(i > n/2) {System.out.println(n + "是素数");}
}
法四:
public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int i;for (i = 2;i <= Math.sqrt(n);i++) {if(n%i == 0) {//System.out.println("n不是素数:"+n);break;}}if(i > Math.sqrt(n)) {System.out.println(n + "是素数");}
}