Javascript JS while循环在&;之后不读取条件&;
下午好, 我试图创建两个随机数,条件如下:它们都在10到99之间,第二个数比第一个小。 我编写了以下代码:Javascript JS while循环在&;之后不读取条件&;,javascript,Javascript,下午好, 我试图创建两个随机数,条件如下:它们都在10到99之间,第二个数比第一个小。 我编写了以下代码: window.onload= function() { var x = Math.floor(Math.random() * 100); while (x < 10) { x = Math.floor(Math.random() * 100) } var
window.onload= function() {
var x = Math.floor(Math.random() * 100);
while (x < 10) {
x = Math.floor(Math.random() * 100)
}
var y = Math.floor(Math.random() * 100);
while ((y < 10) && (y<x)) {
y = Math.floor(Math.random() * 100)
};
document.getElementById("var1").innerHTML = x;
document.getElementById("var2").innerHTML = y;
var z = parseInt( x) - parseInt(y);
document.getElementById("var3").innerHTML= z;
};
window.onload=function(){
var x=Math.floor(Math.random()*100);
而(x<10){
x=Math.floor(Math.random()*100)
}
变量y=数学地板(数学随机()*100);
而((y<10)&(y您不应该循环,而是通常只执行以下操作:
x = Math.floor(Math.random() * 90) + 10
y = Math.floor(Math.random() * (x - 10)) + 10
您不应循环,而应通常执行以下操作:
x = Math.floor(Math.random() * 90) + 10
y = Math.floor(Math.random() * (x - 10)) + 10
您需要为数字引入偏移量-对于第一个数字,它从10开始;对于第二个数字,它从第一个数字开始
let min = 10,
first = Math.floor(Math.random() * (100 - min)) + min,
second = Math.floor(Math.random() * (100 - first)) + first;
实际上,这允许两个数字相等。否则,如果第一个数字是100,那么第二个数字不能是任何数字。您需要为数字引入偏移量-对于第一个数字,它从10开始;对于第二个数字,它从第一个数字开始
let min = 10,
first = Math.floor(Math.random() * (100 - min)) + min,
second = Math.floor(Math.random() * (100 - first)) + first;
实际上,这允许两个数字相等。否则,如果第一个数字是100,第二个数字就不能是任何数字。这可能会解决您的问题:
window.onload=function(){
var x=Math.floor(Math.random()*100);
而(x<10){
x=Math.floor(Math.random()*100)
}
变量y=数学地板(数学随机()*100);
而(y<10 | | y>x){
y=Math.floor(Math.random()*100)
};
document.getElementById(“var1”).innerHTML=x;
document.getElementById(“var2”).innerHTML=y;
var z=parseInt(x)-parseInt(y);
document.getElementById(“var3”).innerHTML=z
};
这可能会解决您的问题:
window.onload=function(){
var x=Math.floor(Math.random()*100);
而(x<10){
x=Math.floor(Math.random()*100)
}
变量y=数学地板(数学随机()*100);
而(y<10 | | y>x){
y=Math.floor(Math.random()*100)
};
document.getElementById(“var1”).innerHTML=x;
document.getElementById(“var2”).innerHTML=y;
var z=parseInt(x)-parseInt(y);
document.getElementById(“var3”).innerHTML=z
}希望这能回答你的问题
function randomNumbers() {
var temp_list;
temp_list = [
Math.floor(Math.random() * 90 + 10),
Math.floor(Math.random() * 90 + 10)
];
temp_list.sort();
var x = temp_list[1];
var y = temp_list[0];
document.getElementById("var1").innerHTML = x;
document.getElementById("var2").innerHTML = y;
var z = parseInt(x) - parseInt(y);
document.getElementById("var3").innerHTML= z;
}
如果不希望x和y中的值相同:
function randomNumbers() {
var temp_list;
do {
temp_list = [
Math.floor(Math.random() * 90 + 10),
Math.floor(Math.random() * 90 + 10)
];
} while (temp_list[0] == temp_list[1])
temp_list.sort();
var x = temp_list[1];
var y = temp_list[0];
document.getElementById("var1").innerHTML = x;
document.getElementById("var2").innerHTML = y;
var z = parseInt(x) - parseInt(y);
document.getElementById("var3").innerHTML = z;
}
希望这能回答你的问题
function randomNumbers() {
var temp_list;
temp_list = [
Math.floor(Math.random() * 90 + 10),
Math.floor(Math.random() * 90 + 10)
];
temp_list.sort();
var x = temp_list[1];
var y = temp_list[0];
document.getElementById("var1").innerHTML = x;
document.getElementById("var2").innerHTML = y;
var z = parseInt(x) - parseInt(y);
document.getElementById("var3").innerHTML= z;
}
如果不希望x和y中的值相同:
function randomNumbers() {
var temp_list;
do {
temp_list = [
Math.floor(Math.random() * 90 + 10),
Math.floor(Math.random() * 90 + 10)
];
} while (temp_list[0] == temp_list[1])
temp_list.sort();
var x = temp_list[1];
var y = temp_list[0];
document.getElementById("var1").innerHTML = x;
document.getElementById("var2").innerHTML = y;
var z = parseInt(x) - parseInt(y);
document.getElementById("var3").innerHTML = z;
}
让我们看一下这里的案例
首先将x设置为0到100之间的任意值,然后,如果该值恰好小于10,则继续将其设置为另一个介于0到100之间的随机数,直到它大于10为止
首先,如果每次的结果都小于10,它可能会永远循环(不太可能,但在可能性范围内),如果你想得到一个10-100之间的随机数,只需在乘法中加上10,然后去掉10,就像
Math.floor(Math.random()*(100-10))+10
for (let r = 0; r < 100; r ++) {
x = Math.floor(Math.random() * 100);
while (x < 10) {
x = Math.floor(Math.random() * 100);
}
y = Math.floor(Math.random() * 100);
while ((y < 10) || (y > x)) {
y = Math.floor(Math.random() * 100);
}
}
而且你也不需要一个while循环
不管怎样,一旦x被设置为10到100之间的值,让我们看看你还做了什么,假设x为90
然后将y设置为0到100之间的任意一个随机值(起初),假设结果为3
然后你先检查它是否小于10,然后继续加。同样,首先你可以使用上面相同的等式得到10-100之间的值,但无论如何,让我们假设y是95
问题是,将其保持在10以下的while循环与小于x的条件放在一起,这意味着如果它在第一位大于10,则while循环的第二个条件,即保持其小于x,将永远不会执行,因此如果y开始时为39或11到38之间的任何值,则whi中不会发生任何事情le循环,因为小于10的条件从未满足
因此,如果要保持while循环的状态,可以将两个while循环嵌套在一起,首先将其保持在x以下,然后在内部循环上,将其保持在10以上
但是您不必为此使用while循环
首先,将x的值设置为
var x=Math.floor(Math.random()*(100-10))+10
for (let r = 0; r < 100; r ++) {
x = Math.floor(Math.random() * 100);
while (x < 10) {
x = Math.floor(Math.random() * 100);
}
y = Math.floor(Math.random() * 100);
while ((y < 10) || (y > x)) {
y = Math.floor(Math.random() * 100);
}
}
之后,只需将y保持在10到x的范围内,因此对这些起始值和结束值使用相同的公式
var y=Math.floor(Math.random()*(x-10))+10
让我们来看看这里的案例
for (let r = 0; r < 100; r ++) {
x = Math.floor(Math.random() * 100);
while (x < 10) {
x = Math.floor(Math.random() * 100);
}
y = Math.floor(Math.random() * 100);
while ((y < 10) || (y > x)) {
y = Math.floor(Math.random() * 100);
}
}
首先将x设置为0到100之间的任意值,然后,如果该值恰好小于10,则继续将其设置为另一个介于0到100之间的随机数,直到它大于10为止
首先,如果每次的结果都小于10,它可能会永远循环(不太可能,但在可能性范围内),如果你想得到一个10-100之间的随机数,只需在乘法中加上10,然后去掉10,就像
Math.floor(Math.random()*(100-10))+10
for (let r = 0; r < 100; r ++) {
x = Math.floor(Math.random() * 100);
while (x < 10) {
x = Math.floor(Math.random() * 100);
}
y = Math.floor(Math.random() * 100);
while ((y < 10) || (y > x)) {
y = Math.floor(Math.random() * 100);
}
}
而且你也不需要一个while循环
不管怎样,一旦x被设置为10到100之间的值,让我们看看你还做了什么,假设x为90
然后将y设置为0到100之间的任意一个随机值(起初),假设结果为3
然后你先检查它是否小于10,然后继续加。同样,首先你可以使用上面相同的等式得到10-100之间的值,但无论如何,让我们假设y是95
问题是,将其保持在10以下的while循环与小于x的条件放在一起,这意味着如果它在第一位大于10,则while循环的第二个条件,即保持其小于x,将永远不会执行,因此如果y开始时为39或11到38之间的任何值,则whi中不会发生任何事情le loop,因为小于