IF语句将变量值带入开关的问题

时间:2019-05-15 06:23:18

标签: javascript if-statement switch-statement

我正在尝试提示用户必须输入1到4范围内的数字(通过do-while循环再次提示,直到他们输入正确的数字为止),然后该数字将对应于开关中的一个盒子(它将打开一个新窗口)。在测试中,代码一直正确执行,直到我的ELSE语句为止,在该代码中,代码突然“忘记”了用户输入的号码,以退出while循环,因此在每种情况下都不会将用户带到代码上在开关中。

  

测试:提示=用户输入5,新提示:无效选择!请输入1到4之间的数字,用户输入4,提示关闭。

     

谁能看到为什么IF语句中的reDirect变量不进入开关的原因?

我会提出任何建议,但我需要给用户“自由度”以输入超出范围的数字,以证明我可以处理错误。

我尝试在if语句中实现新变量,但它们也被排除在关闭开关之外。

    function reDirect() {
       var reDirect = 0;
       reDirect = parseInt(prompt("Input number between 1 and 4", "1"));
           if (reDirect >=5 || reDirect <=0) {
               var reDirect = 0
               do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1"));
               while (reDirect >=5 || reDirect <=0);
           } 
           else
           {
           var element = document.getElementById("reDirect");
                 switch (reDirect) {
                      default:
                      break;    
                      case 1:
               window.open("URL","_blank","height=800px, width=800px");
                      break;
        }
    }

};

6 个答案:

答案 0 :(得分:0)

听起来您不想有条件运行switch-您总是要运行switch, ve确认输入有效。

只需将switchelse中移出,它就可以运行,无论如何:

function reDirect() {
  var reDirect = 0;
  reDirect = parseInt(prompt("Input number between 1 and 4", "1"));
  if (reDirect >= 5 || reDirect <= 0) {
    do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1"));
    while (reDirect >= 5 || reDirect <= 0);
  }
  var element = document.getElementById("reDirect");
  switch (reDirect) {
    case 1:
      window.open("URL", "_blank", "height=800px, width=800px");
      break;
    // other cases
    default: break; // ???
  }
}

还请注意,函数声明块的末尾不应包含;,并且由于reDirect是在函数的顶部声明的,因此您不应尝试通过以下方式重新声明它: var块内的if。 (您也可能希望将案例2-4放入案例中,或者在default案例中放入更有意义的内容)

答案 1 :(得分:0)

default位于switch的结尾:

switch (reDirect) {
    case 1:
        window.open("URL","_blank","height=800px, width=800px");
        break;
    default:
        break;
}

答案 2 :(得分:0)

将默认值放在switch语句的末尾:

function reDirect() {
      var reDirect = 0;
      reDirect = parseInt(prompt('Input number between 1 and 4', '1'));
      if (reDirect >= 5 || reDirect <= 0) {
        var reDirect = 0;
        do
          reDirect = parseInt(prompt('Invalid! Input number between 1 and 4', '1'));
        while (reDirect >= 5 || reDirect <= 0);
      } else {
        var element = document.getElementById('reDirect');
        switch (reDirect) {
          case 1:
            window.open('URL', '_blank', 'height=800px, width=800px');
            break;
          default:
            break;
        }
      }
    }

答案 3 :(得分:0)

一些更改:

  • 您可以更改获取值的循环,使其只有一部分。

  • 您需要在default语句的末尾移动switch部分,因为如果找到此语句,则会立即使用此部分,并且switch语句将结束。

  • 要解析数字,您需要使用parseInt的基数。

  • 为防止使用NaN,您需要切换条件,该条件现在也包含NaN值。

function reDirect() {
    var reDirect;
    do {
        reDirect = parseInt(prompt("Input number between 1 and 4", "1"), 10);
    } while (!(reDirect >= 1 && reDirect <= 4))
    //var element = document.getElementById("reDirect");
    console.log(reDirect);
    switch (reDirect) {
        case 1:
            window.open("URL", "_blank", "height=800px, width=800px");
            break;
        default:
            break;
    }
}

reDirect();

答案 4 :(得分:0)

在if语句中,您检查reDirect是否为> = 5 || <= 0并进行do while循环,而不会传递给else语句中的内容绘制窗口

function reDirect() {
   var reDirect = 0;
   reDirect = parseInt(prompt("Input number between 1 and 4", "1"));
       if (reDirect >=5 || reDirect <=0) {
           var reDirect = 0
           do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1"));
           while (reDirect >=5 || reDirect <=0);
draw();
       } 
       else
       {
draw();
}
}
function draw(){
       var element = document.getElementById("reDirect");
             switch (reDirect) {
                  default:
                  break;    
                  case 1:
           window.open("URL","_blank","height=800px, width=800px");
                  break;
}

答案 5 :(得分:0)

实际上,您已经定义了变量reDirect并将其初始化为0。如果要声明它,则为该变量。

function reDirect() {
       var reDirect = 0;
       reDirect = parseInt(prompt("Input number between 1 and 4", "1"));
           if (reDirect >=5 || reDirect <=0) {
               //var reDirect = 0
               reDirect = 0
               do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1"));
               while (reDirect >=5 || reDirect <=0);
           } 
           else
           {
           var element = document.getElementById("reDirect");
                 switch (reDirect) {
                      default:
                      break;    
                      case 1:
               window.open("URL","_blank","height=800px, width=800px");
                      break;
        }
    }

删除“ var reDirect”后,您的代码即可正常工作

function reDirect() {
       var reDirect = 0;
       reDirect = parseInt(prompt("Input number between 1 and 4", "1"));
           if (reDirect >=5 || reDirect <=0) {
               reDirect = 0
               do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1"));
               while (reDirect >=5 || reDirect <=0);
           } 
           else
           {
           var element = document.getElementById("reDirect");
                 switch (reDirect) {
                      default:
                      break;    
                      case 1:
                      console.log(1)
               window.open("URL","_blank","height=800px, width=800px");
                      break;
        }
    }
    }
reDirect()
<p id="reDirect">
what
</p>