所有!
我是编程新手并尝试编写一个while循环,它将返回包含所有数字1到n的乘积。我无法让我的代码正常工作;它不断返回奇怪的数字。
我认为问题在于while循环的第一行。它不会成倍增加,但我不知道为什么。
这是我写的代码:
var n = 7;
var multiplier = 1;
while (multiplier <= n){
multiplier = (multiplier * multiplier+1);
if (n < 6){
multiplier+= 2;
}
else {
multiplier++;
};
};
console.log(multiplier);
答案 0 :(得分:0)
问题是你使用{
"error": {
"type": "invalid_request_error",
"message": "You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY'). See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/."
}
}
变量,你用它来存储结果,而你需要使用一个单独的变量来存储结果并将其用作计数器变量,如
multiplier
如果你看下面的循环,在第一次迭代结束时乘数为3,第二次循环结束为11,高于7,所以循环存在。
var n = 5;
var multiplier = 1;
var result = 1;
while (multiplier <= n) {
result *= multiplier++;
};
document.body.innerHTML = (result);
var n = 7;
var multiplier = 1;
while (multiplier <= n) {
multiplier = (multiplier * multiplier + 1);
if (n < 6) {
multiplier += 2;
} else {
multiplier++;
};
snippet.log('loop: ' + multiplier)
};
snippet.log('result: ' + multiplier);