统计个位数字

int Count_Digit ( const int N, const int D )
{
int n,i,b=0;
n=N;
if(n<0)
{
n=-n;
}
do{
  i=n%10;
  if(i==D)
  b++;
  n/=10;
}while(n>0);   //记住这里有分号
return b;
}

上面代码为正确代码,必须用do while语句,否则报错,错误在下面:

int Count_Digit ( const int N, const int D )
{
int n,i,b=0;
n=N;
if(n<0)
{
n=-n;
}
while(n>0)
{
  i=n%10;
  if(i==D)
  {
    b++;
  }
  n/=10;

return b;
}

用while报错图片:

 

 

posted @ 2022-10-22 15:49  宠柳娇花  阅读(46)  评论(0)    收藏  举报