用C++循环做正确的循环
当输入一个介于1到7之间的值时,程序应检查该值是否为1到7,以及输入的数字数组中的值是否为0,但该程序的问题是,如果值[0]或其他数组为0,则程序没有循环,它将继续运行用C++循环做正确的循环,c++,do-while,binary-operators,C++,Do While,Binary Operators,当输入一个介于1到7之间的值时,程序应检查该值是否为1到7,以及输入的数字数组中的值是否为0,但该程序的问题是,如果值[0]或其他数组为0,则程序没有循环,它将继续运行 do { cout << "Player 1\n"; cout << "Please choose your location (1 to 7) => "; cin >>
do
{
cout << "Player 1\n";
cout << "Please choose your location (1 to 7) => ";
cin >> h1in ; // player 1 enters a value
cin.clear();// used to clear the value if a str value is entered
cin.ignore(1000,'\n');// used to clear the value if a str value is entered
if ((h1in<1)||(h1in>7))
{
cout << "You entered the wrong input \n\n";
}
if(house[h1in-1]==0)
{
cout<<"jde";
}
} while((house[h1in-1]!=0)&(h1in<1)||(h1in>7));
if (h1in==1)
{
for(int i=1;i<7;i++){
house[0]--;
house[i]++;
if(i==6){
storehouse[0]++;
seeds--;
house[0]--;
}
printout();
}
}
else if(h1in==2)
{
for(int i=1;i<7;i++){
house[1]--;
house[i+1]++;
if(i==6){
storehouse[0]++;
seeds--;
house[1]--;
}
printout();
}
}
else if(h1in==3)
{
for(int i=1;i<7;i++)
{
house[2]--;
house[i+2]++;
if(i==6){
storehouse[0]++;
seeds--;
house[2]--;
}
printout();
}
}
else if(h1in==4)
{
for(int i=1;i<7;i++)
{
house[3]--;
house[i+3]++;
if(i==6){
storehouse[0]++;
seeds--;
house[3]--;
}
printout();
}
}
else if(h1in==5)
{
for(int i=1;i<7;i++)
{
house[4]--;
house[i+4]++;
if(i==6){
storehouse[0]++;
seeds--;
house[4]--;
}
printout();
}
}
else if(h1in==6)
{
for(int i=1;i<7;i++)
{
house[5]--;
house[i+5]++;
if(i==6){
storehouse[0]++;
seeds--;
house[5]--;
}
printout();
}
}
else if(h1in==7)
{
for(int i=1;i<7;i++)
{
house[6]--;
house[i+6]++;
if(i==6){
storehouse[0]++;
seeds--;
house[6]--;
}
printout();
}
}
&是按位和。它在操作数的每一位上执行二进制和。您需要逻辑and的&&运算符
您还应添加一些括号以形成最后一行:
while( (house[h1in-1]!=0) && ((h1in<1)||(h1in>7)));
还是没有working@Teeban你的问题不是很清楚。此外,您不会显示房屋[h1in-1]被分配到的位置。也许你根本不想,你可能在寻找或者代替。我现在更喜欢使用名字,或者,或者不是由标准C++提供的,使代码更清晰易读,避免像写作那样的灾难,而不是& &。使用Visual C++,这是一个在这个领域有点挑战,你可以使用强制包含使这个方法工作。