紧急求助呀~~~一道作业题:java用一维数组输出随机1000个1~100内整数,求输出的整数每个整数出现的次数

2025-02-28 13:17:02
推荐回答(4个)
回答1:

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;

public class Test3 {
public static void main(String[] args) {
//jdk 5.0+
int[] arrInt = new int[1000];
Map map = new HashMap();
Random r = new Random();
for(int i=0;i<1000;i++){
arrInt[i] = r.nextInt(100)+1;
if(map.containsKey(arrInt[i])){
map.put(arrInt[i], map.get(arrInt[i])+1);
}else{
map.put(arrInt[i], 1);
}
}
Iterable> it = map.entrySet();
int c = 0;
for (Entry entry : it) {
System.out.println(entry.getKey()+" 出现了 "+entry.getValue()+"次");
c += entry.getValue();
}
System.out.println("总计数:"+c);
}
}

回答2:

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;
public class cishu {
public static void main(String[] args) {
int[] arrInt = new int[1000];
Map map = new HashMap();
Random r = new Random();
for(int i=0;i<1000;i++)
{
arrInt[i] = r.nextInt(100)+1;
if(map.containsKey(arrInt[i]))
{
map.put(arrInt[i], map.get(arrInt[i])+1);
}
else
{
map.put(arrInt[i], 1);
}
}
Iterable> it = map.entrySet();
int c = 0;
for (Entry entry : it)
{
System.out.println(entry.getKey()+" 出现了 "+entry.getValue()+"次");
c += entry.getValue();
}
System.out.println(""+c);
}
}
保证可以输出。记得存储的名字用cishu.java

回答3:

public class Test {

public static void main(String[] args) {
int[] arr = new int[101];
for(int i=1; i<= 1000; i++) {
arr[getRandomInteger()] = arr[getRandomInteger()] + 1;
}
for(int i=1; i<= 100; i++)
System.out.println(i + ":" + arr[i] + "次");
}

public static int getRandomInteger() {
return (int) Math.round(Math.random() * 100);
}
}

回答4:

用概率啊,10次