Java 如何从映射中分配元素,以便元素的总和不应增加上限
下面是我对适当注释的问题,您需要在循环中编写逻辑,在循环中必须执行以下操作: 1-打印每个状态的随机计数,但计数不得超过地图中的实际计数(statusCount)。 2-应包括列表中的所有状态。 3-如果我将所有打印计数相加,则应等于sampleSize,即100Java 如何从映射中分配元素,以便元素的总和不应增加上限,java,logic,Java,Logic,下面是我对适当注释的问题,您需要在循环中编写逻辑,在循环中必须执行以下操作: 1-打印每个状态的随机计数,但计数不得超过地图中的实际计数(statusCount)。 2-应包括列表中的所有状态。 3-如果我将所有打印计数相加,则应等于sampleSize,即100 public static void main(String[] args) { //Upper bound int sampleSize = 100; //All
public static void main(String[] args) {
//Upper bound
int sampleSize = 100;
//All statuses
final List<Integer> statuses = new ArrayList<Integer>();
statuses.add(15100);
statuses.add(15500);
statuses.add(15300);
statuses.add(15000);
//All status count
final Map<Integer, Integer> statusCount = new HashMap<Integer, Integer>();
statusCount.put(15100, 70);
statusCount.put(15500, 200);
statusCount.put(15300, 370);
statusCount.put(15000, 20);
//Print random counts from each statuses.
//All statuses should be included.
//If i add all the printed count it should be equal to sampleSize i.e. 100
for(Integer status : statuses) {
}
}
publicstaticvoidmain(字符串[]args){
//上限
int sampleSize=100;
//所有状态
最终列表状态=新建ArrayList();
状态。添加(15100);
状态。添加(15500);
状态。添加(15300);
状态。添加(15000);
//所有状态计数
最终映射状态计数=新建HashMap();
statusCount.put(15100,70);
statusCount.put(15500200);
statusCount.put(15300370);
statusCount.put(15000,20);
//打印每个状态的随机计数。
//应包括所有状态。
//如果我将所有打印计数相加,则应等于sampleSize,即100
对于(整数状态:状态){
}
}
嗯,我倾向于在控制台上打印时使用printf
。当然,您需要一个循环来打印数组,例如
for (int i = 0; i < _countof(statuses); i++)
{
printf(statuses[i]);
}
for(int i=0;i<\u countof(status);i++)
{
printf(状态[i]);
}
我建议不要包含//请在此处编写代码。
如果您需要他人帮助。这也不是c