首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在每次for循环更新值时在HTML文档上主动显示统计数据?

如何在每次for循环更新值时在HTML文档上主动显示统计数据?
EN

Stack Overflow用户
提问于 2021-03-07 07:24:05
回答 1查看 82关注 0票数 1

免责声明:非常新的HTML和javascript。还有,很抱歉格式化,我习惯了lua。

我正在尝试这样做,以便每次for循环重复并拉出提示符时,它都会主动更新和显示HTML文档上的统计信息。

统计数据指的是:

  • :用户输入攻击数量后的总伤害,他们要做的攻击数量,龙对用户
  • 造成的总伤害,用户攻击完成后的龙的健康状况,龙攻击完成后的用户健康,

此外,我有一个问题,当龙或用户获胜时,所显示的生命值是上次攻击之前的健康状态。

任何帮助都是非常感谢的。提前谢谢。

代码语言:javascript
复制
var userDmg = document.body.querySelector(".userDamage")
var dragDmg = document.body.querySelector(".dragonDamage")

var userHthTotal = document.body.querySelector(".userHealth")
var dragHthTotal = document.body.querySelector(".dragonHealth")

var userOut = document.body.querySelector(".userOutcome")
var dragOut = document.body.querySelector(".dragonOutcome")



for (var userHealth = 50, dragHealth = 100; userHealth > 0 && dragHealth > 0;)

  {

var userInput = Number(prompt("A dragon has appeared! How many hits do you want to deal?"))    

userHthTotal.innerHTML = "Your health: " +userHealth
dragHthTotal.innerHTML = "Dragon's health: " +dragHealth

if(userInput > 5)
  {userOut.innerHTML = "You exhausted yourself and died."
  userHealth = 0}
else if(userInput <= 0)
  {userOut.innerHTML = "The dragon was disappointed with your non-exstant attack. You died."
  userHealth = 0}
else if(userInput === 5)
  {userDmg.innerHTML = "Your attacks were weak. You deal 1 damage."
  dragDmg.innerHTML = "The dragon is able to land a critical hit and deals 20 damage."
  dragHealth = dragHealth - 1
  userHealth = userHealth - 20}
else if(userInput === 1 || userInput === 2 || userInput === 3 || userInput === 4)
  {var userAttackDmg = Number(userInput) * (Math.floor(Math.random() * 20))
  userDmg.innerHTML = "You dealt " +userAttackDmg+ " damage!"
  dragHealth = dragHealth - userAttackDmg
  
  var dragAttackDmg = Math.floor(Math.random() * 20) + 10
  dragDmg.innerHTML = "The dragon dealt " +dragAttackDmg+ " damage!"
  userHealth = userHealth - dragAttackDmg}
else
  {userOut.innerHTML = "You confused yourself. You died."
  userHealth = 0}

 if(userHealth < 0)
  {userOut.innerHTML = "Tough battle, but the dragon kills you."
  dragOut.innerHTML = "The dragon goes on to pay a visit to your village."}

if(dragHealth < 0)
  {userOut.innerHTML = "You are victorious!"
  dragOut.innerHTML = "However, watch out, the spirit of the dragon will probably haunt you for the rest of your life."}
    
  }
EN

回答 1

Stack Overflow用户

发布于 2021-03-07 07:56:43

您希望移动您的状态更新(这2行:)

代码语言:javascript
复制
userHthTotal.innerHTML = "Your health: " +userHealth
dragHthTotal.innerHTML = "Dragon's health: " +dragHealth

在你的大如果块下面,甚至一直到你的for循环的底部。

另外,对于最后两个if-块,我怀疑您希望使用<=而不是<

代码语言:javascript
复制
if (userHealth <= 0) ...

if (dragHealth <= 0) ...

调整后的版本:

代码语言:javascript
复制
var userDmg = document.body.querySelector(".userDamage")
var dragDmg = document.body.querySelector(".dragonDamage")

var userHthTotal = document.body.querySelector(".userHealth")
var dragHthTotal = document.body.querySelector(".dragonHealth")

var userOut = document.body.querySelector(".userOutcome")
var dragOut = document.body.querySelector(".dragonOutcome")



for (var userHealth = 50, dragHealth = 100; userHealth > 0 && dragHealth > 0;)

{

    var userInput = Number(prompt("A dragon has appeared! How many hits do you want to deal?"))


    if (userInput > 5) {
        userOut.innerHTML = "You exhausted yourself and died."
        userHealth = 0
    } else if (userInput <= 0) {
        userOut.innerHTML = "The dragon was disappointed with your non-exstant attack. You died."
        userHealth = 0
    } else if (userInput === 5) {
        userDmg.innerHTML = "Your attacks were weak. You deal 1 damage."
        dragDmg.innerHTML = "The dragon is able to land a critical hit and deals 20 damage."
        dragHealth = dragHealth - 1
        userHealth = userHealth - 20
    } else if (userInput === 1 || userInput === 2 || userInput === 3 || userInput === 4) {
        var userAttackDmg = Number(userInput) * (Math.floor(Math.random() * 20))
        userDmg.innerHTML = "You dealt " + userAttackDmg + " damage!"
        dragHealth = dragHealth - userAttackDmg

        var dragAttackDmg = Math.floor(Math.random() * 20) + 10
        dragDmg.innerHTML = "The dragon dealt " + dragAttackDmg + " damage!"
        userHealth = userHealth - dragAttackDmg
    } else {
        userOut.innerHTML = "You confused yourself. You died."
        userHealth = 0
    }

    userHthTotal.innerHTML = "Your health: " + userHealth
    dragHthTotal.innerHTML = "Dragon's health: " + dragHealth

    if (userHealth <= 0) {
        userOut.innerHTML = "Tough battle, but the dragon kills you."
        dragOut.innerHTML = "The dragon goes on to pay a visit to your village."
    }

    if (dragHealth <= 0) {
        userOut.innerHTML = "You are victorious!"
        dragOut.innerHTML = "However, watch out, the spirit of the dragon will probably haunt you for the rest of your life."
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66514157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档