Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在随机句子生成器中,生成器总是返回相同的故障_Javascript - Fatal编程技术网

Javascript 在随机句子生成器中,生成器总是返回相同的故障

Javascript 在随机句子生成器中,生成器总是返回相同的故障,javascript,Javascript,嗯,它总是奇怪地返回一个不是我想要的句子。这是密码 function erase(){document.open()} alert("welcome to random sentence genereator and your sentence is on the screenand in the console below") var b =Math.floor(Math.random() * 5); if (b="1"){var a=("i

嗯,它总是奇怪地返回一个不是我想要的句子。这是密码

function erase(){document.open()}
alert("welcome to random sentence genereator and your sentence is on the screenand in the console below")
var b =Math.floor(Math.random() * 5);
if (b="1"){var a=("i ")}
else if(b="2"){var a=("a bean ")}
else if(b="3"){var a =("a xbox ")}
else if(b="0"){var a =("sport drink ")}
else if (b="4"){var a =("some grass ")}
else if(b="5"){var a =("a rock ")}
var e=Math.floor(Math.random() * 4);
if(e="0"){var p =("fell on ")}
else if(e="1"){var p =("jumped on ")}
else if(e="2"){var p =("bounced over ")}
else if(e="3"){var p =("slid on")}
else if(e="4"){var p =("slipped on")}
var n=Math.floor(Math.random() * 6);
if(n="0"){var q=("a mountain ")}
if(n="1"){var q =("a candlestick ")}
else if(n="2"){var q =("a raw potato ")}
else if(n="3"){var q=("a book ")}
else if(n="4"){var q=("a glass bottle ")}
else if(n="5"){var q =("water ")}
else if(n="6"){var q =("fish soup ")}
var o=Math.floor(Math.random() * 3);
if(o="0"){var v =("in the dark")}
if (o="1"){var v =("in the light")}
if(o="2"){var v=("in the middle of nowhere")}
if(o="3"){var v=("in a giant lemon")}
document.write(a+p+q+v)

为什么你让它工作,总是奇怪地返回一个句子。plz awnser

b
是一个数字,因此您应该将其与
b=='3'
b=='3
进行比较。
另外,
Math.floor(Math.random()*5)
会生成一个介于0和4之间的数字(而且永远不会是5),所以你永远不会用它去
b==5

b
是一个数字,所以你应该将它与
b='3'
b==3
进行比较。
除此之外,
Math.floor(Math.random()*5)
生成一个介于0和4之间的数字(而且永远不会是5),因此您永远不会使用此函数转到
b==5

尝试以下代码:

function erase() {
    document.open()
}
alert("welcome to random sentence genereator and your sentence is on the screen and in the console below")
var b = Math.floor(Math.random() * 5);
if (b === 1) {
    var a = ("i ")
} else if (b === 2) {
    var a = ("a bean ")
} else if (b === 3) {
    var a = ("a xbox ")
} else if (b === "0") {
    var a = ("sport drink ")
} else if (b === 4) {
    var a = ("some grass ")
} else if (b === 5) {
    var a = ("a rock ")
}
var e = Math.floor(Math.random() * 4);
if (e === 0) {
    var p = ("fell on ")
} else if (e === 1) {
    var p = ("jumped on ")
} else if (e === 2) {
    var p = ("bounced over ")
} else if (e === 3) {
    var p = ("slid on ")
} else if (e === 4) {
    var p = ("slipped on ")
}
var n = Math.floor(Math.random() * 6);
if (n === 0) {
    var q = ("a mountain ")
}
if (n === 1) {
    var q = ("a candlestick ")
} else if (n === 2) {
    var q = ("a raw potato ")
} else if (n === 3) {
    var q = ("a book ")
} else if (n === 4) {
    var q = ("a glass bottle ")
} else if (n === 5) {
    var q = ("water ")
} else if (n === 6) {
    var q = ("fish soup ")
}
var o = Math.floor(Math.random() * 3);
if (o === 0) {
    var v = ("in the dark")
}
if (o === 1) {
    var v = ("in the light")
}
if (o === 2) {
    var v = ("in the middle of nowhere")
}
if (o === 3) {
    var v = ("in a giant lemon")
}
document.write(a + p + q + v)

尝试以下代码

function erase() {
    document.open()
}
alert("welcome to random sentence genereator and your sentence is on the screen and in the console below")
var b = Math.floor(Math.random() * 5);
if (b === 1) {
    var a = ("i ")
} else if (b === 2) {
    var a = ("a bean ")
} else if (b === 3) {
    var a = ("a xbox ")
} else if (b === "0") {
    var a = ("sport drink ")
} else if (b === 4) {
    var a = ("some grass ")
} else if (b === 5) {
    var a = ("a rock ")
}
var e = Math.floor(Math.random() * 4);
if (e === 0) {
    var p = ("fell on ")
} else if (e === 1) {
    var p = ("jumped on ")
} else if (e === 2) {
    var p = ("bounced over ")
} else if (e === 3) {
    var p = ("slid on ")
} else if (e === 4) {
    var p = ("slipped on ")
}
var n = Math.floor(Math.random() * 6);
if (n === 0) {
    var q = ("a mountain ")
}
if (n === 1) {
    var q = ("a candlestick ")
} else if (n === 2) {
    var q = ("a raw potato ")
} else if (n === 3) {
    var q = ("a book ")
} else if (n === 4) {
    var q = ("a glass bottle ")
} else if (n === 5) {
    var q = ("water ")
} else if (n === 6) {
    var q = ("fish soup ")
}
var o = Math.floor(Math.random() * 3);
if (o === 0) {
    var v = ("in the dark")
}
if (o === 1) {
    var v = ("in the light")
}
if (o === 2) {
    var v = ("in the middle of nowhere")
}
if (o === 3) {
    var v = ("in a giant lemon")
}
document.write(a + p + q + v)

因为比较是通过
==
===
完成的。不要把
=
放在一边,因为你的问题已经有了答案。这里有一个快速提示:尝试格式化代码。您可以在线安装像Prettier这样的工具或使用此工具进行操作:另一个提示:如果您在If条件块之外初始化变量,则无需多次执行(即
var a;If(b==1){a=“i”}
)并且使变量的预期范围更加清晰,因为您试图在定义它们的块之外使用它。因为比较是通过
=
==
完成的。不要把
=
放在一边,因为你的问题已经有了答案。这里有一个快速提示:尝试格式化代码。您可以在线安装像Prettier这样的工具或使用此工具进行操作:另一个提示:如果您在If条件块之外初始化变量,则无需多次执行(即
var a;If(b==1){a=“i”}
)并且使变量的预期范围更加清晰,因为您试图在定义它们的块之外使用它。仍然返回相同的内容如果您将每个
b=“1”
更改为
b==“1”
,它应该可以工作。e、n和o也是如此。@codesforkids@Ivar认为使用==是不好的做法。始终使用==@RahulGurujala我的示例只是为了说明它应该有效。我大体上同意您的观点,严格的相等比较是首选。但仍然返回相同的结果。如果您将每个
b=“1”
更改为
b==“1”
,则应该可以使用该方法。e、n和o也是如此。@codesforkids@Ivar认为使用==是不好的做法。始终使用==@RahulGurujala我的示例只是为了说明它应该有效。我大体上同意你的观点,严格的相等比较是首选。随机数总是生成数字而不是字符串,你是将数字与字符串进行比较,这可能会导致你在未来的项目中出错。随机数总是生成数字而不是字符串,你是将数字与字符串进行比较,这可能会导致你在未来的项目中出错。