Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不能让两个if-else语句工作吗?检查字符串和数字_Javascript - Fatal编程技术网

Javascript 不能让两个if-else语句工作吗?检查字符串和数字

Javascript 不能让两个if-else语句工作吗?检查字符串和数字,javascript,Javascript,我想运行这两个if-else语句,但我无法让它们工作?请帮忙 如果输入失败,返回开始并再次询问 function table(){ var num = prompt("please enter any number"); if (num <= 0 && typeof num != 'string') { alert("invalid number or Zero") ; table(); } else {

我想运行这两个if-else语句,但我无法让它们工作?请帮忙

  • 如果输入失败,返回开始并再次询问

    function table(){
        var num = prompt("please enter any number");
        if (num <= 0 && typeof num != 'string') {
            alert("invalid number or Zero") ;
            table();
        } else {
            alert("ok") ;
        }
    }
    
    table();
    
    function text(){
        var txt = prompt("please enter rock or scissors or paper");
        if (txt != "rock" || "scissors" || "paper") {
            alert("failed") ;
            table();
        } else {
            alert("ok") ;
        }
    }
    
    text();
    

  • 谢谢。

    请尝试以下语法:

    var num = parseInt( prompt("please enter any number") );
    if (num <= 0) {
      // ...
    }
    

    只需尝试以下语法:

    var num = parseInt( prompt("please enter any number") );
    if (num <= 0) {
      // ...
    }
    

    只需尝试以下语法:

    var num = parseInt( prompt("please enter any number") );
    if (num <= 0) {
      // ...
    }
    

    只需尝试以下语法:

    var num = parseInt( prompt("please enter any number") );
    if (num <= 0) {
      // ...
    }
    

    类型
    您从
    提示符
    返回的结果将始终是
    “string”
    (用户单击确定或按Enter键)或
    “object”
    (用户单击取消或按Esc键),因为
    类型为null的
    “object”
    ,而
    提示符
    返回键入的内容,或取消时的
    null
    。这就是第一个
    if
    的问题所在

    如果不接受空白,则简单检查为

    var num = prompt(...);
    if (!num) {
        // User clicked Cancel or didn't type anything
    }
    
    …然后使用
    +num
    num
    转换为一个数字,或者使用
    parseInt(num,10)
    这样做,如果您希望确保以10为基数并忽略数字后面的任何文本(
    parseInt(“42foo”,10)
    42
    而不是
    NaN
    +“42foo”
    NaN

    第二个
    if
    的问题是您必须重复您正在测试的内容并使用
    &&
    而不是
    |

    if (txt != "rock" && txt != "scissors" && txt != "paper"){
    
    “如果txt不是石头,txt不是剪刀,并且…”

    开关在那里可能有用:

    switch (txt) {
        case "rock":
        case "scissors":
        case "paper":
            alert("ok") ;
            break;
        default:
            alert("failed") ;
            table();
            break;
    }
    

    类型
    您从
    提示符
    返回的结果将始终是
    “string”
    (用户单击确定或按Enter键)或
    “object”
    (用户单击取消或按Esc键),因为
    类型为null的
    “object”
    ,而
    提示符
    返回键入的内容,或取消时的
    null
    。这就是第一个
    if
    的问题所在

    如果不接受空白,则简单检查为

    var num = prompt(...);
    if (!num) {
        // User clicked Cancel or didn't type anything
    }
    
    …然后使用
    +num
    num
    转换为一个数字,或者使用
    parseInt(num,10)
    这样做,如果您希望确保以10为基数并忽略数字后面的任何文本(
    parseInt(“42foo”,10)
    42
    而不是
    NaN
    +“42foo”
    NaN

    第二个
    if
    的问题是您必须重复您正在测试的内容并使用
    &&
    而不是
    |

    if (txt != "rock" && txt != "scissors" && txt != "paper"){
    
    “如果txt不是石头,txt不是剪刀,并且…”

    开关在那里可能有用:

    switch (txt) {
        case "rock":
        case "scissors":
        case "paper":
            alert("ok") ;
            break;
        default:
            alert("failed") ;
            table();
            break;
    }
    

    类型
    您从
    提示符
    返回的结果将始终是
    “string”
    (用户单击确定或按Enter键)或
    “object”
    (用户单击取消或按Esc键),因为
    类型为null的
    “object”
    ,而
    提示符
    返回键入的内容,或取消时的
    null
    。这就是第一个
    if
    的问题所在

    如果不接受空白,则简单检查为

    var num = prompt(...);
    if (!num) {
        // User clicked Cancel or didn't type anything
    }
    
    …然后使用
    +num
    num
    转换为一个数字,或者使用
    parseInt(num,10)
    这样做,如果您希望确保以10为基数并忽略数字后面的任何文本(
    parseInt(“42foo”,10)
    42
    而不是
    NaN
    +“42foo”
    NaN

    第二个
    if
    的问题是您必须重复您正在测试的内容并使用
    &&
    而不是
    |

    if (txt != "rock" && txt != "scissors" && txt != "paper"){
    
    “如果txt不是石头,txt不是剪刀,并且…”

    开关在那里可能有用:

    switch (txt) {
        case "rock":
        case "scissors":
        case "paper":
            alert("ok") ;
            break;
        default:
            alert("failed") ;
            table();
            break;
    }
    

    类型
    您从
    提示符
    返回的结果将始终是
    “string”
    (用户单击确定或按Enter键)或
    “object”
    (用户单击取消或按Esc键),因为
    类型为null的
    “object”
    ,而
    提示符
    返回键入的内容,或取消时的
    null
    。这就是第一个
    if
    的问题所在

    如果不接受空白,则简单检查为

    var num = prompt(...);
    if (!num) {
        // User clicked Cancel or didn't type anything
    }
    
    …然后使用
    +num
    num
    转换为一个数字,或者使用
    parseInt(num,10)
    这样做,如果您希望确保以10为基数并忽略数字后面的任何文本(
    parseInt(“42foo”,10)
    42
    而不是
    NaN
    +“42foo”
    NaN

    第二个
    if
    的问题是您必须重复您正在测试的内容并使用
    &&
    而不是
    |

    if (txt != "rock" && txt != "scissors" && txt != "paper"){
    
    “如果txt不是石头,txt不是剪刀,并且…”

    开关在那里可能有用:

    switch (txt) {
        case "rock":
        case "scissors":
        case "paper":
            alert("ok") ;
            break;
        default:
            alert("failed") ;
            table();
            break;
    }
    

    1.
    prompt
    的返回值始终是字符串。2.了解逻辑运算符如何工作<代码>“剪刀”
    “纸”
    总是正确的,因为你让它们自己站着,而不是将它们与
    txt
    @Siguza比较:
    提示符的返回值通常是一个字符串。如果用户取消,则为
    null
    (这不是字符串)。@T.J.Crowder好的,我到了。我应该说“从来没有一个数字”…1。
    prompt
    的返回值始终是字符串。2.了解逻辑运算符如何工作<代码>“剪刀”
    “纸”
    总是正确的,因为你让它们自己站着,而不是将它们与
    txt
    @Siguza比较:
    提示符的返回值通常是一个字符串。如果用户取消,则为
    null
    (这不是字符串)。@T.J.Crowder好的,我到了。我应该说“从来没有一个数字”…1。
    prompt
    的返回值始终是字符串。2.了解逻辑运算符如何工作<代码>“剪刀”
    “纸”
    永远是正确的,因为你让它们自己站着,而不是将它们与
    txt
    @Siguza:返回值进行比较