java中怎么求随机产生50个10以内的整数,输出其中重复次数最多的数?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中怎么求随机产生50个10以内的整数,输出其中重复次数最多的数?相关的知识,希望对你有一定的参考价值。

public class test05

public static void main(String [] args)
int array [] = new int[10];
int max = 0;
int index = 0;
for(int i=0;i<50;i++)
int temp = (int)(Math.random()*10);
array[temp]++;


for(int i=0;i<array.length;i++)
if(array[i] > max)
max = array[i];
index = i;


System.out.println("出现次数最多的数:" +index+" "+"出现了:" +max +"次");

参考技术A Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=1;i<=50;i++)
int r = (int)(Math.random()*11);
if(map.containsKey(r))
map.put(r, map.get(r)+1);
else
map.put(r, 1);

Iterator<Map.Entry<Integer, Integer>> iter = map.entrySet().iterator();
Map.Entry<Integer, Integer> max = null;
while(iter.hasNext())
Map.Entry<Integer, Integer> next = iter.next();
if(max == null || next.getValue() > max.getValue())
max = next;

System.out.println(max.getKey() + " " + max.getValue());
参考技术B 记忆力急速下降,有何办法。

以上是关于java中怎么求随机产生50个10以内的整数,输出其中重复次数最多的数?的主要内容,如果未能解决你的问题,请参考以下文章