嘿我试图在矩阵a [3] [3]的随机位置上添加元素; 问题是当它在矩阵上保持2-3个自由位置时,程序崩溃。拒绝退出while循环。
char a[3][3]={{ '.' , '.' , '.' },{ '.' , '.' , '.' },{ '.' , '.' , '.' }};
if(selected!=true&&postpone!=true&&elemctr<8)
{
Orow=rand()%3;
Ocol=rand()%3;
while(1)
{
cout << "in while";
cout << endl;
if(a[Orow][Ocol]=='.')
{
if(Orow!=Xrow||Ocol!=Xcol)
{
fct();
ocalled=true;
}
}
else
{
//-------------------
for(int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
if(a[i][j]=='.')
{
Orow=i;
Ocol=j;
found=true;
break;
}
if(found==true)
{
break;
}
}
if(found==true)
{
break;
}
}
}
if(ocalled==true)
{
break;
}
}
}
怎么做,以避免陷入while循环,并完成随机元素随机取得的所有矩阵位置?
答案 0 :(得分:1)
干杯