import java.util.HashMap; import java.util.Map; public class Test_A26 { //判断一个数是不是素数 public static boolean isPrime(int num){ if(num<=1){ return false; } for(int i=2;i<=Math.sqrt(num);i++){ if(num%i==0){ return false; } } return true; } //统计数组中出现的频率 public static Map<Integer,Integer> countFrequency(int[] array){ Map<Integer,Integer> frequencyMap=new HashMap<>(); for(int num:array){ if(frequencyMap.containsKey(num)){ frequencyMap.put(num,frequencyMap.get(num)+1); }else { frequencyMap.put(num,1); } } return frequencyMap; } public static void main(String [] args){ int[] array={3,5,7,4,2,85,34,56,23,53}; System.out.println("素数判断:"); for(int num:array){ if(isPrime(num)){ System.out.println(num+"是素数"); }else{ System.out.println(num+"不是素数"); } } Map<Integer,Integer> frequencyMap=countFrequency(array); System.out.println("\n数组中的素数出现的频率:"); for(Map.Entry<Integer,Integer> entry:frequencyMap.entrySet()){ System.out.println(entry.getKey()+"出现的次数为:"+entry.getValue()); } } }