【问题标题】:my simple rng code is not working. Why is that so?我的简单 rng 代码不起作用。为什么呢?
【发布时间】:2021-04-06 08:58:10
【问题描述】:

我是 Javascript 新手,到目前为止,我刚刚看了一个 3 小时的教程并参加了一些私人编程课程。我被分配了我的第一个项目,在那里我尝试模拟两支军队之间的战斗。这是代码。

var myArmy = ["arcieri", "fanteria", "cavalleria", "morale"]  // <--- my Army brigades
function rng_loop() {
Math.floor(Math.random * myArmy.length)  // <---- the rng function
}

while (myArmyStrength > 0 && enemyArmyStrength > 0) {
  rng_loop()
} if (rng_loop() > 1) {
  enemyArmyStrength - 25
} else if (rng_loop() < 1) {
myArmyStrength - 20
}

/* this is what was supposed to be my loop generating random numbers and killing off both armies based on the value of said random numbers until one of the two armies got to 0. myArmyStrength and enemyArmyStrength were specified earlier on. */

if (myArmyStrength > 0 && enemyArmyStrength <= 0) {
console.log("we won the battle! Roma invicta!")
} else if (myArmyStrength <= 0 && enemyArmyStrength > 0) {
console.log("We lost the battle...")
} 
/* this if else statement was supposed to console.log a message either announcing a victory or defeat to the enemy army, but it's not console.logging anything. Why is that so? */

【问题讨论】:

  • rng_loop 缺少退货声明
  • 我注意到的第一件事是您没有从nrg_loop() 返回值。在该函数体中使用return Math.floor(....
  • 另外Math.random() 是一种方法。你必须使用() 来调用它

标签: javascript math random while-loop


【解决方案1】:

四个问题

1。你的函数需要返回一些东西

目前你正在计算一些东西并把结果扔掉。你必须做的是return 结果。另外,正如上面一位评论者所指出的,您需要在Math.random 之后使用(),因为您希望执行函数Math.random

function rng_loop() {
    return Math.floor(Math.random() * myArmy.length) 
}

2。不要每次循环调用函数 3 次:只调用一次,然后存储值

我在下面的代码中称它为fightOutcome。如果不这样存储,每次调用 rng_loop() 函数都会返回不同的数字。第一个将被丢弃,更重要的是,后两个调用(对于“>1”和“ 和 第二个!或者两者都没有。

3。 'while'的{}中的代码需要包含所有'ifs'

while (myArmyStrength > 0 && enemyArmyStrength > 0) {
    const fightOutcome = rng_loop()
    if (fightOutcome > 1) {
      enemyArmyStrength -= 25
    } else if (fightOutcome < 1) {
      myArmyStrength -= 20
    }
}

4。您将 `-=` 拼写错误为 `-`

您试图使用

来减少两个军队强度变量
enemyArmyStrength -= 25;

但不幸的是你只写了

enemyArmyStrength - 25

所做的只是计算你的敌人ArmyStrength - 25,然后把结果扔掉,而不是把它写到敌人ArmyStrength 变量中。

奖励建议:不要单独测试 1 等

这有两个问题。首先,该值恰好 1 的可能性非常小。这里极不可能,但当您为“>”和“非>”编写其他类型的代码时,它会更重要。正确的方法是一个为“=1”。 (或者您可以将= 移动到&lt; 一侧)。

第二,容易出错。 (事实上​​,你已经做到了!)

我的建议是只测试一半的问题,并使用else 来处理另一种可能性。

 if (fightOutcome > 1) {
  enemyArmyStrength - 25
} else {
  myArmyStrength - 20
}
您的后续问题 `很抱歉打扰您,我应用了您建议的修改,但遗憾的是控制台仍然没有记录任何结果。知道为什么吗?

我现在也解释了第四个错误,抱歉。

let myArmyStrength = 20;
let enemyArmyStrength = 30;

var myArmy = ["arcieri", "fanteria", "cavalleria", "morale"]

function rng_loop() {
  return Math.floor(Math.random() * myArmy.length) 
}

while (myArmyStrength > 0 && enemyArmyStrength > 0) {
  if (rng_loop() > 1) {
    enemyArmyStrength -= 25
  } else {
    myArmyStrength -= 20
  }
}

if (enemyArmyStrength <= 0) {
  console.log("we won the battle! Roma invicta!")
} else  {
  console.log("We lost the battle...")
}

如果你运行上面的片段,它现在可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2014-01-23
    相关资源
    最近更新 更多