刚开始时是一名业务分析师,但是我需要学习Javascript来完成其他工作:/我刚开始使用代码学院,所以我对Javascript的了解仍然很有限。
无论如何,我试图在一个函数中生成一个随机数,但似乎它生成的数字始终是相同的数字/结果。
var randNum = Math.floor(Math.random()*3)
function getChoice ()
{
if (randNum) = 0) {console.log("Choice 1")}
else if (randNum = 1) {console.log("Choice 2")}
else (console.log("Choice 3")}
}
getComputerChoice()
console.log(randNum)
代码似乎总是转到选项2。但是,当我///选择2时,它转到选项3。由于某种原因,它无法显示选项1。我还尝试过仅将randNum变量用于最后console.log(randNum)行检查它是否真正随机化。它可以,但是仅当它不与功能一起使用时
var randNum = Math.floor(Math.random()*3)
function getChoice ()
{
var choiceRandNum = Math.floor(Math.random()*3)
if (choiceRandNum) = 0) {console.log("Choice 1")}
else if (choiceRandNum = 1) {console.log("Choice 2")}
else (console.log("Choice 3")}
}
getChoice()
console.log(randNum)
对于上面的代码,我尝试为该函数制作一个不同的变量(但实际上是相同的randNum)。现在,最后一行可以显示不同的数字(0,1,2),但该函数仍不会随机化。可以进行任何编辑吗?请不要使用过于复杂的解决方案,因为我仍然不了解很多Java。
答案 0 :(得分:1)
您在函数外部声明了变量,因此无论您多久调用一次函数,结果都将保持不变。您的代码中也有一些语法错误。使用===
代替=
并删除一些括号:
function getChoice () {
var randNum = Math.floor(Math.random()*3);
if (randNum === 0) {
console.log("Choice 1");
} else if (randNum === 1) {
console.log("Choice 2");
} else {
console.log("Choice 3");
}
}
getChoice();
这应该给您想要的结果
答案 1 :(得分:1)
当前,您的代码中存在多个错误。从原始代码开始,到我认为自己想要的结束,以编程方式修复每个问题的方法如下:
1:您的代码中有很多语法错误。首先,这是一个干净的版本,因此更容易了解发生了什么:
var randNum = Math.floor(Math.random() * 3)
function getChoice() {
if (randNum) = 0) {
console.log("Choice 1")
}
else if (randNum = 1) {
console.log("Choice 2")
}
else (console.log("Choice 3")
}
}
getComputerChoice()
console.log(randNum)
然后,您可以删除所有多余的括号,并添加其他括号:
var randNum = Math.floor(Math.random() * 3)
function getChoice() {
if (randNum = 0) {
console.log("Choice 1")
}
else if (randNum = 1) {
console.log("Choice 2")
}
else {
console.log("Choice 3")
}
}
getComputerChoice()
console.log(randNum)
最后,您可以在==
语句中添加等价运算符=
而不是赋值运算符if
,并添加分号:
var randNum = Math.floor(Math.random() * 3);
function getChoice() {
if (randNum == 0) {
console.log("Choice 1");
}
else if (randNum == 1) {
console.log("Choice 2");
}
else {
console.log("Choice 3");
}
}
getComputerChoice();
console.log(randNum);
2:函数的名称与调用的名称不同。将函数重命名为getComputerChoice()
:
var randNum = Math.floor(Math.random() * 3);
function getComputerChoice() {
if (randNum == 0) {
console.log("Choice 1");
}
else if (randNum == 1) {
console.log("Choice 2");
}
else {
console.log("Choice 3");
}
}
getComputerChoice();
console.log(randNum);
3:您只需声明一次随机数,所以无论调用该函数多少次,它都不会改变。要解决此问题,请在函数外部将randNum
声明为0
,然后在函数中进行更改:
var randNum = 0;
function getComputerChoice() {
randNum = Math.floor(Math.random() * 3);
if (randNum == 0) {
console.log("Choice 1");
}
else if (randNum == 1) {
console.log("Choice 2");
}
else {
console.log("Choice 3");
}
}
getComputerChoice();
console.log(randNum);
现在您的代码可以使用了。这是一个有效的代码段:
var randNum = 0;
function getComputerChoice() {
randNum = Math.floor(Math.random() * 3);
if (randNum == 0) {
console.log("Choice 1");
} else if (randNum == 1) {
console.log("Choice 2");
} else {
console.log("Choice 3");
}
console.log(randNum);
}
<button onclick="getComputerChoice()">Random Number</button>
注意:我提供的代码片段将console.log(randNum)
放在函数内部,因此如果使用randNum == 1
,则输出将是:
Choice 2
1