【问题标题】:Exit from program with conditional if使用条件 if 退出程序
【发布时间】:2015-08-15 20:43:33
【问题描述】:

我有一个疑问,我想在有条件的情况下退出程序,例如:

    var number = prompt("Choice a number between 1 and 6");
    var random = Math.floor(Math.random() * 6) + 1;
    if (parseInt(number) > 6){
        document.write("<p>Please, choice a number between 1 and 6</p>")
        // Here, I want to exit of conditional and program. 
    } else {
        if()
    }
    if() {
    } else {}

这是可能的。

非常感谢

【问题讨论】:

标签: javascript if-statement


【解决方案1】:

也许你改变了比较的逻辑

if (parseInt(number) <= 6) {
    // do other stuff
} else {
    document.write("<p>Please, choice a number between 1 and 6</p>")
    // Here, I want to exit of conditional and program. 
}
// this is the end of the program

【讨论】:

    【解决方案2】:

    程序执行的主线程属于浏览器。 JavaScript 按需调用; JavaScript 代码不控制浏览器。所以 JavaScript 代码不能退出控制的主线程或浏览器。 在您的情况下,您可以简单地使用 return 关键字。请记住,JS 是一种函数式语言,因此您的脚本会在函数结束时结束。

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 1970-01-01
      • 2015-03-06
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多