【发布时间】:2023-03-16 22:32:01
【问题描述】:
我想生成唯一的随机数并根据这些随机数添加项目。这是我的代码:
问题是当我使用代码results.contains(randomNb) 验证生成的数字是否存在于数组中时:
int nbRandom = ui->randoomNumberSpinBox->value();
//nbRandom is the number of the random numbers we want
int i = 1;
int results[1000];
while ( i < nbRandom ){
int randomNb = qrand() % ((nbPepoles + 1) - 1) + 1;
if(!results.contains(randomNb)){
//if randomNb generated is not in the array...
ui->resultsListWidget->addItem(pepoles[randomNb]);
results[i] = randomNb;
//We add the new randomNb in the array
i++;
}
}
【问题讨论】:
-
...你的问题是...什么?
-
您似乎离找到可行的解决方案只有一步之遥。您所需要的只是一个检查特定数字是否在数组(特定大小)中的函数。然后,您可以将
results.contains(randomNb)替换为对该函数的调用。你有什么理由不能自己写那个函数吗?这就是你寻求帮助的原因吗? -
哦对不起,我已经编辑了我的问题^^
-
results.contains(randomNb)不是合法的 C++。那不可能是你的代码。 -
它不起作用,这就是问题所在!为什么我应该正确??