如何检查数组的所有值是否等于0? [英] How to check if all the values of an array are equal to 0?

查看:773
本文介绍了如何检查数组的所有值是否等于0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序的上下文是一个涉及钉子和光盘的游戏.用户输入桩的数量(最多20个)和每个桩上的碟片的数量(最大10个).假设有足够的碟片可移动,两名玩家每回合在一个钉上来回移动任何数量的碟片.删除最后一张光盘的播放器丢失.

The context of the program is a game involving pegs and discs. The user inputs the amount of pegs (max of 20) and the amount of discs on each peg (max of 10). Two players go back and forth removing any amount of discs on a single peg each turn, given that there are enough discs to remove on that peg. The player to remove the last disc loses.

光盘的数量存储在阵列中,其中阵列的索引对应于桩号.我有一个布尔函数,用于检查钉子是否没有光盘,这表明有人赢了.我的代码中存在一些逻辑错误,但我无法弄清楚它是什么:

The number of discs are stored in an array, where the index of the array corresponds the peg number. I have a boolean function that checks whether or not the pegs are empty of discs, implying someone has won. There is some logical error in my code but I can't figure out what it is:

bool checkPegs(int array[], int size)
{
  int checker(0);
  for (int i = 0; i < size; i++)
  {
      if(array[i] = 0)
      {
        return true;
      }
      else
      {
        return false;
      }
  }
}

推荐答案

bool checkPegs(int array[], int size)
{
  for (int i = 0; i < size; i++)
  {
      if(array[i] != 0)
      {
        return false;
      }
  }
  return true;
}

这篇关于如何检查数组的所有值是否等于0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆