編寫一個Java應用程序,對用戶輸入的任意一組字符如{1,3,4,7,2,1,1,5,2},輸出其中出現次數最多且數值最大的字符,並顯示其出現次數。
import java.util.Arrays;
/*
* 1、 編寫一個Java應用程序,對用戶輸入的任意一組字符如{1,3,4,7,2,1,1,5,2},
* 輸出其中出現次數最多且數值最大的字符,並顯示其出現次數。
*/
public class T1 {
int[] a={1,2,4,7,2,1,1,5,2};
int firstNum,secondNum,count,secondCount=0;
static int num;
public ThreeB1(){
Arrays.sort(a);
System.out.println("Bisggest:"+a[a.length-1]);
for(int i=0;i<a.length;i++){
firstNum=a[i];
count=0;
for(int k=0;k<a.length;k++){
secondNum=a[k];
if(firstNum==secondNum){
count++;
}
}
if(secondCount<=count){
secondCount=count;
num=firstNum;
}
}
System.out.println("Count:"+secondCount+" num:"+num);
}
public static void main(String[] args) {
new ThreeB1();
}
}