我在使用javascript函数,循环应该运行10次,在这10次中,应该抛出一个基于,+和 - 操作的随机问题,也应该有4个“+”问题,3“ - ”问题和3“”问题。而循环不应运行超过10倍,有人请帧这样的逻辑...循环逻辑
到目前为止我的代码:
<script type="text/javascript">
var op=new Array();
var addCount=0;
var subCount=0;
var mulCount=0;
var counter=0;
var no;
op[0]="+";
op[1]="-";
op[2]="x";
Array.prototype.chooseRandom = function()
{
return this[Math.floor(Math.random() * this.length)];
};
var a = [1, 2];
var b = [0, 2];
var c = [0, 1];
no=Math.floor((Math.random()*3));
while(addCount < 4|| subCount < 3 || mulCount < 3)
{
no=Math.floor((Math.random()*3));
if(no==0)
{
if(addCount<4)
{
addCount++;
var op1=Math.floor(Math.random() * (99 - 10+1)) + 10;
var op2=Math.floor(Math.random() * (99 - 10+1)) + 10;
}
else
{
no=a.chooseRandom();
}
}
else if(no==1)
{
if(subCount<3)
{
subCount++;
var no1=Math.floor(Math.random() * (99 - 10+1)) + 10;
var no2=Math.floor(Math.random() * (99 - 10+1)) + 10;
if(no1>no2)
{
var op1=no1;
var op2=no2;
}
else
{
var op1=no2;
var op2=no1;
}
}
else
{
no=b.chooseRandom();
}
}
else if(no==2)
{
if(mulCount<3)
{
mulCount++;
var op1=Math.floor(Math.random() * (99 - 10+1)) + 10;
var op2=Math.floor(Math.random() * (9 - 1+1)) + 1;
}
else
{
no=c.choseRandom();
}
}
counter++;
}
</script>
发布你迄今试过的代码 – polin 2013-02-19 07:59:33
@polin,这里的代码,请查看 – Hazel 2013-02-19 08:09:09