代码如下:
package zuoye5;
import java.util.Arrays;
import java.util.Random;
public class Shuzi {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int size = 100;
int[] figures = new int[size];
int[] times = new int[size];
Random random = new Random();
for (int i = 0; i < figures.length; i++) {
figures[i] = random.nextInt(size) + 1;// 产生1-size中的数字
System.out.print(figures[i] + ",");
}
System.out.println("\n升序排序后");
Arrays.sort(figures);
int num = figures[0];// 取出升序后的第一个元素与之后的元素进行比较
System.out.print(figures[0] + ",");
int cishu = 1;
for (int i = 1; i < figures.length; i++) {
System.out.print(figures[i] + ",");
if (num == figures[i]) {
cishu++;
times[figures[i] - 1] = cishu;// 设置该元素的下表和出现的次数
} else {
cishu = 1;
}
num = figures[i];
}
System.out.println();
cishu = times[0];
num = 0;
for (int i = 1; i < times.length; i++) {
if (cishu <= times[i]) {
cishu = times[i];
num = i;
}
}
System.out.println((num + 1) + "出现次数最多" + cishu + "次");
}
}
效果如下:
是不是很简单呢,赶紧自己去试一试吧!