【问题标题】:do while Loop with & not looping properly c++ [closed]用&没有正确循环c ++做while循环[关闭]
【发布时间】:2017-01-18 05:32:16
【问题描述】:

当输入 1 到 7 之间的值时,程序应该检查该值是否为 1 到 7 以及输入的数字数组中的值是否不为 0 的条件,但是这个程序的问题是,如果值 House [0] 或其他数组为 0 程序没有循环它一直运行

    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();
                }
            }

【问题讨论】:

  • 我现在更喜欢使用标准 C++ 提供的名称andornot,以使代码更清晰易读,并避免像写&amp; 而不是写&amp; &amp;&amp;。使用 Visual C++,在这方面有点挑战,您可以使用 &lt;ios646.h&gt; 的强制包含来使这种方法有效。

标签: c++ do-while binary-operators


【解决方案1】:

&amp; 是按位的and。它在操作数的每一位上执行二进制and。你想要&amp;&amp; 运算符,它是逻辑and

您还应该在最后一行添加一些括号:

while( (house[h1in-1]!=0) && ((h1in<1)||(h1in>7)));

【讨论】:

  • 还是不行
  • @Teeban 你的问题不是很清楚。你也没有显示house[h1in-1] 被分配到哪里。也许你根本不想要and,你可能正在寻找or
猜你喜欢
  • 2016-05-03
  • 2013-04-07
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多