- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先我想说这个问题可能有类似的问题,但我的问题非常不同。
我正在生成四个随机数,但是当其中一个数字低于 max/10
或高于 max/1.2
时,我想再次加载该函数,直到得到正确的随机数。我在脚本中这样写:
if (first < max / 10 || second < max / 10 || third < max / 10 || fourth < max / 10) { reken(max); }
if (first > max / 1.2 || second > max / 1.2 || third > max / 1.2 || fourth > max / 1.2) { reken(max); }
但这对我不起作用。我得到的数字低于 10,有时高达 85。当在 if 语句后面添加一些愚蠢的内容(例如: if(){ asdhbjka }
)时,代码会崩溃并等待另一秒来运行脚本。这样我就得到了好的数字。所以我知道我的 if
语句有效,只是 reken(max);
Soo..希望你能帮助我。
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function reken(max) {
var status = document.getElementById('status');
var max = 100
var first = random(1, max / 2.0 - 3);
var second = random(1, max / 1.5 - 2 - first);
var third = random(1, max / 1.2 - 1 - first - second);
var fourth = max - first - second - third;
if (first < max / 10 || second < max / 10 || third < max / 10 || fourth < max / 10) {
reken(max);
}
if (first > max / 1.2 || second > max / 1.2 || third > max / 1.2 || fourth > max / 1.2) {
reken(max);
}
status.innerHTML = first + " - " + second + " - " + third + " - " + fourth + " / " + (first + second + third + fourth );
}
var animateInterval = setInterval(reken,1000);
<p id="status"></p>
最佳答案
你的问题可以在你的递归调用中找到。
让我们简单一点:
function findNumber()
{
var number = random(0,10);
if (number < 5)
findNumber();
document.GetElementById("labelForMyNumber").innerText(number);
}
让我们运行该代码..会发生什么:
Step 1: number = 6
Step 2: if fails (number > 5)
Step 3: label get text:
6
好的,我们现在再试一次:
Step 1: number = 4
Step 2: If succeeds (number < 5)
Step 3: we call the function again
Step 4: new number, this time 8
Step 5: If fails (8 > 5)
Step 6: We change the text to 8.
BUT!! Step 7: We return to the first function! Number is now 4 again (see step 1)
Step 8: We change the text to number which is 4.
你看到了吗?当你进行递归时,你总是返回到之前的函数,并继续执行该代码。
现在,如何解决这个问题?
简单的方法是这样的:
function findNumber()
{
var number = random(0,10);
if (number < 5)
findNumber();
else
document.GetElementById("labelForMyNumber").innerText(number);
}
<小时/>
按照这个逻辑,你的代码应该是:
function reken(max) {
var status = document.getElementById('status');
var max = 100
var first = random(1, max / 2.0 - 3);
var second = random(1, max / 1.5 - 2 - first);
var third = random(1, max / 1.2 - 1 - first - second);
var fourth = max - first - second - third;
if ((first < max / 10 || second < max / 10 || third < max / 10 || fourth < max / 10) || (first > max / 1.2 || second > max / 1.2 || third > max / 1.2 || fourth > max / 1.2) {
reken(max);
}
else {
status.innerHTML = first + " - " + second + " - " + third + " - " + fourth + " / " + (first + second + third + fourth );
}
}
关于javascript - 生成四个随机数,使其相加达到最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30053265/
我知道可以创建 object.__mul__(self, other)启用两个对象的自定义乘法的方法。然而,正如 python 文档中所解释的那样, to evaluate the expressio
我无法找到一种好方法来查找列中的所有负条目并将它们向上移动到列中,将它们与现有条目相加(即从当前条目中减去负条目),直到所有值都是正值。 重要的是,最终数据帧没有负值 & 所有以前的负条目 = 0。此
我想将 var string 中的所有元素相加。我确实尝试过这个,但现在我想对每个元素都这样做。 var operator = document.getElementById("operation")
“任何长度的正数表示为数字字符数组,因此介于‘0’和‘9’之间。我们知道最重要的密码位于数组索引 0 的位置。 例子: - 号码是 10282 - 数组将是数字 = [1,0,2,8,2] 考虑到这一
这是我的查询: UPDATE qanda SET amount = amount + 1000 WHERE id = ? AND type = 0; 默认情况下, amount 列为 null,当我执
不同列的值为1时,每列加10分。 要求将累加的结果汇总到表格的一列中。但总分不得超过20分。因此,如果总和达到 30,列中的结果将始终为 20。 SELECT CASE WHE
public static void main(String[] args) { Vector vec = new Vector(); vec.add(new
我正在尝试使用 js 或 jquery 将 +20,2 添加到带有点 9990.95 的总和或带有逗号 9990,95 的总和 9990.95 var price = $( '.sum' ).text
我如何编写一个函数来将二维数组中每一行的内容相加?要添加每一列的内容?我的代码(到目前为止): #include using namespace std; const int QUARTER = 4
这个问题在这里已经有了答案: Count the number of set bits in a 32-bit integer (65 个答案) 关闭 8 年前。 我有这段代码可以获取数字的二进制表
创建了一个 Div,在其中我有 label 元素和 input 元素,我想在每个 div 中获得不同的标签值。如何重用我的 div 组件而不是再次编写相同的代码。 我尝试在 Stackoverflow
所以我在构建代码时遇到了这个问题。这个问题 This work is based on operator overloading, you need to build a string calcula
为了好玩并进一步了解 float 的工作原理,我尝试制作一个函数,它接受两个单精度 float ,并将它们相加。 到目前为止,我所做的对于相同符号的数字非常有效,但当数字具有相反的符号时,它就会分崩离
我想添加两个 map 以及以下行为。 如果键存在 -> 将两个键值相加。 如果键不存在 -> 插入对映射。 我看过一些标准库算法。即转换,但似乎没有做我想要的。 取自此LINK template
我的程序中有两个整数;我们称它们为“a”和“b”。我想将它们加在一起并得到另一个整数。这些是常规的 Python int 对象。我在想;我如何将它们与 Twisted 一起添加?某处是否有特殊的 pe
因此,我必须创建这个程序,从文件中读取多个“工资”,然后将所有工资相加以返回一个“总工资”,并计算所有工资的平均值。 我目前拥有的代码如下: package uploadTask7_countingS
我的结果是这样的: +-------+--------+--------+-----+--------------+ | Count | Equipe | IdTeam | Id | Nam
我有以下数据框: df2 = pd.DataFrame({'season':[1,1,1,2,2,2,3,3],'value' : [-2, 3,1,5,8,6,7,5], 'avail':[3,3,
所以我正在过滤数据(所有字符串),并希望显示以下内容: 第一个字母包含您的搜索的搜索词(例如,在搜索 'Br' 时,我想查看所有以 'Br' 开头的词,例如 Break , 坏了,...) 字符串中包
我试图通过以下关系(仅显示父级)来说明一对多关系的总和: @Entity @Table(name = "Parent") public class Parent implements Serializ
我是一名优秀的程序员,十分优秀!