Java java 利用函数实现素数判断

 1 package ch16_2021_12_22;
 2 
 3 import java.util.Scanner;
 4 
 5 public class test01 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub    
 9         Scanner sc = new Scanner(System.in);
10         System.out.println("请输入一个数:");
11         int a = sc.nextInt();
12         
13         if(prime(a)){
14             System.out.println(a+"是素数");
15         }else{
16             
17         System.out.println(a+"不是素数");
18         }
19     }
20     public static  boolean prime(int x){
21        
22         int i=2;
23         while(i<=x-1){
24             if(x%i==0){
25                 break;     
26             }
27             i++;    
28         }
29         if(i==x){
30             
31             return true;        
32         }else{            
33             return false;
34         }
35     
36     }
37 
38 }

 CF功夫熊猫CF功夫熊猫CF功夫熊猫CF功夫熊猫

posted @ 2021-12-22 19:05  程序员小贤  阅读(324)  评论(0)    收藏  举报