编写一个 Java 应用程序,统计数组{1,3,4,7,2,1,1,5,2,5,7,2,1,1,3},统 计显示每种数字其出现的次数以及出现最多和最少次数的数字
1 public class NumberCount { 2 public static void main(String arg[]) 3 { 4 int a[]= {1,3,4,7,2,1,1,5,2,5,7,2,1,1,3}; 5 int currNumber,currMaxCount=1,currMinCount=1; 6 int max=0,min=0; 7 int currCount[]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 8 for(int i=0;i<a.length;i++) 9 { 10 currNumber=a[i]; 11 for(int j=0;j<a.length;j++) 12 { 13 if(a[j]==currNumber) 14 currCount[i]=currCount[i]+1; 15 } 16 } 17 for(int i=0;i<a.length;i++) 18 { 19 20 if(currMaxCount<=currCount[i]) 21 { 22 currMaxCount=currCount[i]; 23 max=a[i]; 24 } 25 } 26 for(int i=0;i<a.length;i++) 27 { 28 29 if(currMinCount>=currCount[i]) 30 { 31 currMinCount=currCount[i]; 32 min=a[i]; 33 } 34 } 35 36 37 for(int i=0;i<a.length;i++) 38 System.out.println(a[i]+"出现次数:"+currCount[i]); 39 System.out.print("出现最多的数字为"+max+" 出现次数最少的数字为:"+min); 40 } 41 42 }