我的 switch 语句的目的是获取从 0 到 7 的随机生成输入的输入,以执行七个任务中的一个。这是我的代码:
switch(move)
{
case 0:
//up
movement = 1;
next = vf.plv(x,y,movement,cp,np);
//hit cell membrane
if(next == 0)
{
move = vf.mv(0);
}
else
{ cout<<j<<"\t"<<x<<"\t"<<y+1<<endl;
virus[j].setvpoint(x,y+1,j,movement);
break;
}
case 1:
//up-right
movement = 2;
next = vf.plv(x,y,movement,cp,np);
//hit cell membrane
if(next == 0)
{
move = vf.mv(1);
}
else
{
cout<<j<<"\t"<<x+1<<"\t"<<y+1<<endl;
virus[j].setvpoint(x+1,y+1,j,movement);
break;
}
case 2:
//right
movement = 3;
next = vf.plv(x,y,movement,cp,np);
//hit cell membrane
if(next == 0)
{
move = vf.mv(2);
}
else
{
cout<<j<<"\t"<<x+1<<"\t"<<y<<endl;
virus[j].setvpoint(x+1,y,j,movement);
break;
}
case 3:
//down-right
movement = 4;
next = vf.plv(x,y,movement,cp,np);
//hit cell membrane
if(next == 0)
{
move = vf.mv(3);
}
else
{
cout<<j<<"\t"<<x+1<<"\t"<<y-1<<endl;
virus[j].setvpoint(x+1,y-1,j,movement);
break;
}
case 4:
//down
movement = 5;
next = vf.plv(x,y,movement,cp,np);
//hit cell membrane
if(next == 0)
{
move = vf.mv(4);
}
else
{
cout<<j<<"\t"<<x<<"\t"<<y-1<<endl;
virus[j].setvpoint(x,y-1,j,movement);
break;
}
case 5:
//down-left
movement = 6;
next = vf.plv(x,y,movement,cp,np);
//hit cell membrane
if(next == 0)
{
move = vf.mv(5);
}
else
{
cout<<j<<"\t"<<x-1<<"\t"<<y-1<<endl;
virus[j].setvpoint(x-1,y-1,j,movement);
break;
}
case 6:
//left
movement = 7;
next = vf.plv(x,y,movement,cp,np);
//hit cell membrane
if(next == 0)
{
move = vf.mv(6);
}
else
{
cout<<j<<"\t"<<x-1<<"\t"<<y<<endl;
virus[j].setvpoint(x-1,y,j,movement);
break;
}
case 7:
//left-up
movement = 8;
next = vf.plv(x,y,movement,cp,np);
//hit cell membrane
if(next == 0)
{
move = vf.mv(7);
}
else
{
cout<<j<<"\t"<<x-1<<"\t"<<y+1<<endl;
virus[j].setvpoint(x-1,y+1,j,movement);
break;
}
default:
cout<<"Default"<<endl
}
next = vf.plv(x,y,movement,cp,np)是一个根据 x 和 y 坐标输出 0 或 1 的函数。将 x 和 y 坐标输入到距离公式中以确定与中心的距离,如果距离为 13 或更大,则 next 将等于 0。如果等于 0,则 move = vf.mv(7)函数将生成一个新的0 到 7 之间的数字,然后将其放回此开关功能
我遇到的问题是 switch 函数将递归生成一个新数字 3 次,然后恢复到默认语句,而不是继续直到它找到小于 13 的距离。我试过做一个 do while 循环,它仍然有没用。
解决此问题的下一步应该是什么?
先感谢您!