gpt4 book ai didi

java - 如何统计彩票中每个开奖号码的金额并将其输出到列表中?

转载 作者:行者123 更新时间:2023-12-01 17:39:21 25 4
gpt4 key购买 nike

我正在编写这个小彩票应用程序。现在的计划是,计算每次彩票迭代期间每个号码被抽取的频率,并将其存储在某个地方。我的猜测是,我需要使用 HashMap,它有 6 个键,并且每次绘制相应的键编号时,值都会增加 1。但我该如何实现这个目标呢?到目前为止我的代码:

public void numberCreator()
{
// creating and initializing a Random generator
Random rand = new Random();
// A HashMap to store the numbers picked.
HashMap hashMap = new HashMap();
// A TreeMap to sort the numbers picked.
TreeMap treeMap = new TreeMap();

// creating an ArrayList which will store the pool of availbale Numbers
List<Integer>numPool = new ArrayList<Integer>();

for (int i=1; i<50; i++){
// add the available Numbers to the pool
numPool.add(i);
hashMap.put(nums[i], 0);
}

// array to store the lotto numbers
int [] nums = new int [6];

for (int i =0; i < nums.length; i++){
int numPoolIndex = rand.nextInt(numPool.size());
nums[i] = numPool.get(numPoolIndex);

// check how often a number has been called and store the new amount in the Map
int counter = hashMap.get

numPool.remove(numPoolIndex);
}

System.out.println(Arrays.toString(nums));

}

也许有人可以告诉我我的想法是否正确,甚至如何正确实现 map ?

最佳答案

HashMap,或者任何类型的映射,在这里都太复杂了。计算数字出现次数所需的只是一个数组。

假设你有 49 个可能的数字,声明一个数组:

int counts = new int[49];
Arrays.fill(counts,0);

然后对于每次抽奖:

int drawnumbers[6]; // put the numbers for this draw in the array
for (int i=0;i<6;++i) {
counts[drawnumbers[i]-1]++;
}

最后打印结果:

for (int i=0;i<49;++i) {
System.out.println("Number "+(i+1)+" occurred "+counts[i]+" times.");
}

关于java - 如何统计彩票中每个开奖号码的金额并将其输出到列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2951431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com