简体   繁体   English

我在使用switch语句时遇到麻烦,特别是使它们反复检查我的代码

[英]I am having trouble with switch statements specifically making them check my code over and over

My code is only running my switch statement once and because of that the page doesn't refresh. 我的代码只运行一次switch语句,因此该页面不会刷新。 This makes it so once the code is run you only have those three buttons to press while what I want it to do it to update the switch statement multiple times since my variable I am checking is updating. 这样一来,一旦运行代码,您就只需要按下这三个按钮,而我想要它执行的操作就可以多次更新switch语句,因为我正在检查的变量正在更新。

  let level = 1
    if (level === 1) {
      alert(level + '. You are walking down a path and see a shadowy figure emerge from the woods, what do you do?')
      switch (level) {
        case 1:
          var button = document.createElement("button");
          button.innerHTML = "Threaten";
          var body = document.getElementsByTagName("body")[0];
          body.appendChild(button);
          button.addEventListener("click", function() {
            alert("You threaten the man but are put to the ground and stabbed, you wake up hours later in a shack on the ocean with a women standing over you what do you do?");
            level += 1

          });
          var button = document.createElement("button");
          button.innerHTML = "Flee";
          var body = document.getElementsByTagName("body")[0];
          body.appendChild(button);
          button.addEventListener("click", function() {
            alert("You run like the wind to escape the man, you reach your house to tell your wife, she calls you crazy and leaves with your kids, what is your move?");
            level += 2
          });
          var button = document.createElement("button");
          button.innerHTML = "Attack";
          var body = document.getElementsByTagName("body")[0];
          body.appendChild(button);
          button.addEventListener("click", function() {
            alert("You take out a knife and plunge it through his chest, he falls to the ground, blood seeping from his vest, you run away, what is your move as you escape into the forest?");
            level += 3
          });
          break;

        case 2:

          var button = document.createElement("button");
          button.innerHTML = "Pay";
          var body = document.getElementsByTagName("body")[0];
          body.appendChild(button);
          button.addEventListener("click", function() {
            alert("did something");
          });
          var button = document.createElement("button");
          button.innerHTML = "Threaten";
          var body = document.getElementsByTagName("body")[0];
          body.appendChild(button);
          button.addEventListener("click", function() {
            alert("did something");
          });

          break;
          default:
          alert('fail');

} }; };

I set up the switch properly but it only running case 1 and is not looking back at case two even though the variable is updating. 我正确设置了开关,但它只运行案例1,即使变量正在更新,它也没有回首案例2。 Do I have to put to switch statement in a for loop? 我是否必须在for循环中放入switch语句?

You might try something like this to shorten and simplify your code: 您可以尝试使用以下方法来缩短和简化代码:

 var levels = [ [ 'You are walking down a path and see a shadowy figure emerge from the woods, what do you do?', {'Threaten': 1, 'Flee': 2, 'Attack': 3} ], [ 'You threaten the man but are put to the ground and stabbed, you wake up hours later in a shack on the ocean with a women standing over you what do you do?', {'Die': 4} ], [ 'You run like the wind to escape the man, you reach your house to tell your wife, she calls you crazy and leaves with your kids, what is your move?', {'Die': 4} ], [ 'You take out a knife and plunge it through his chest, he falls to the ground, blood seeping from his vest, you run away, what is your move as you escape into the forest?', {'Die': 4} ], [ 'You cannot die!', {'Start Over': 0} ], ], q = document.getElementById('q'), b = document.getElementById('b'), level = 0; play(level); function play(n = 0) { q.innerHTML = levels[n][0]; b.innerHTML = ''; for (var i in levels[n][1]) { var button = document.createElement('button'); button.name = levels[n][1][i]; button.innerHTML = i; button.addEventListener('click', function(){play(this.name)}); b.appendChild(button); } } 
 <h3 id="q"></h3> <div id="b"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的碰撞代码有问题 - I am having trouble with my collision code 我无法理解为什么我无法映射我在 React 程序中设置的状态下返回的数据 - I am having trouble understanding why I am not able to .map over the data I am return in the state that I set up in my React program 我格式化switch语句是否错误? - Am I formatting my switch statements wrong? 为什么 if 语句优先于 switch 语句? - Why are if statements preferred over switch statements? 我在制作JavaScript打字机效果时遇到问题 - I am having trouble making a javascript typewriter effect 我正在尝试通过php代码更新记录。 但是我的代码有麻烦 - I am trying to update records by php code. but having trouble in my code 鼠标悬停在选项卡上时遇到麻烦 - Having trouble on mouse over tabs 我有带有粒子的 Sketch.js,但粒子在我的导航栏上,因此链接不可点击 - I am having sketch.js with particles but the particles are over my navbar so the links are not clickable 我是否在此jQuery代码中一再绑定事件? - Am I binding events over and over again in this jQuery code? 我的 JavaScript 代码在尝试使用 keydown 事件处理程序更改类中的变量时遇到问题 - I am having trouble in my JavaScript code trying to change a variable in a class with a keydown event hander
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM