求大神说下用Js的for,产生一个1-1000的随机数,如果该数不能被9整除直接

2024-11-22 12:58:14
推荐回答(1个)
回答1:

function printNum(){
                    for(;;){//类似while(true)
                        var num = parseInt(Math.random(1000) * 1000 + 1);
                        if(num % 9 != 0){
                            console.log(num);
                        }else{
                            alert("取到1~1000内能被9整除的随机数:" + num);
                            break;
                        }
                    }
                }

for(;;)是个死循环,只有内部条件成立才会退出;