0

我开始自己学习 Google Javascript,因为我的第一个代码是做一个交互式计算器,可以通过按下这些方程的一些图像来选择方程。问题是我想在输入意外信息时重复该功能,它会说请重试并重新启动代码。我应该在代码末尾写什么?

else{
    Browser.msgBox("XXXXXXXXXXXXX");
    / *enter the loop code here*
4

1 回答 1

0

一个好方法是将变量设置为null,然后使用while循环将控制用户界面的代码(在您的示例中为消息框)和验证输入的代码分开也很好

function validate_input(user_input){
    if(...your code here...)
    {
        //user input is valid
        return true;
    }else{
        //user input is invalid
        return false;
    }
}

var user_input = null;

//it is necessary to use triple equals to compare to null
while(user_input===null){

    user_input=get_user_input_somehow(); //you specify 
    if(validate_input(user_input)){

    //do nothing
    }
    else{

    Browser.msgBox("xxxxxxx");

    //this line is very necessary
    //if the input is invalid, reset the input to null
        user_input = null;

    }
}

//by this point in the code, the user_inut is assured to be valid
//otherwise the loop would still be running
于 2019-05-08T17:12:14.713 回答