import java.util.Scanner; public class FunctionDemo3 { public static void main(String[] args) { System.out.print("请输入一个整数: "); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); judgePrimeNum(num); } public static void judgePrimeNum(int n) { if(n == 1 || n == 0) { System.out.println(n + " 不是质数!"); } else if (n == 2) { System.out.println(n + " 是质数!"); } else { for (int i = 2; i < n; i++) { if(n % 2 == 0) { System.out.println(n + " 不是质数!"); return; } } System.out.println(n + " 是质数!"); } } }
运行结果:
请输入一个整数: 6
6 不是质数!