为什么我得到0而不是2 [英] Why am I getting 0 instead of 2

查看:234
本文介绍了为什么我得到0而不是2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将给定的整数拆分为数字,并使用递归打印整数中存在的"5"的数字

我尝试过的事情:

split the given integer into digits and print the number of "5" present in the integer using recursion

What I have tried:

#include <stdio.h>
 
int extract (int); 
 
int main()
{
  int Number, r = 0;
 
  printf("\nPlease Enter any number\n");
  scanf("%d", &Number);
 
  r = extract(Number);
 
  printf(" %d", r);
  return 0;
}
 
int extract (int Number)
{
   int Reminder, c=0;
 
  if(Number > 0)
  {
    Reminder = Number % 10;
     if(Number == 5)
     {
         c=c+1;
     }
     else
     {
         c=0;
     }
    extract (Number / 10);
    return c;
  }
 else
   return 0;
}



[edit]添加了代码块-OriginalGriff [/edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

好吧...因为每次遇到 不是 "5".
因此,如果最高有效位是1、2、3、4、6、7、8或9,则无论用户输入中的数字是否为``5'',您都将始终返回0.

看一下自己-使用调试器,它将向您显示正在发生的事情.
在函数的第一行上放置一个断点,然后通过调试器运行代码.然后查看您的代码和数据,并确定应该手动执行的操作.然后,单行检查每一行,以确保您期望发生的事情确实是正确的.如果不是,那就是您遇到问题时,可以回溯(或再次运行并仔细查看)以找出原因.
是时候让您学习一种新的(而且非常非常有用的)技能:调试!
Well...because you set the count to zero every time you meet a digit that isn''t ''5''.
So if the most significant digit is 1, 2, 3, 4, 6, 7, 8, or 9 you will alway return 0 - regardless of the number of ''5'' digits in the user input.

Have a look for yourself - use the debugger and it will show you what is going on.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn''t, that''s when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Time for you to learn a new (and very, very useful) skill: debugging!


我看到您的代码无法正常工作的3个不同原因. 其中之一是:
I see 3 different reasons why your code don''t work.
one of them is:
extract (Number / 10);


在递归调用中,您只是丢弃结果,这是一个错误.
帮自己一个忙,学习如何使用调试器.

当您不了解代码在做什么或为什么要做什么时,答案是调试器.
使用调试器查看您的代码在做什么.它使您能够从1开始执行第1行,并在执行时检查变量,这是一个了不起的学习工具.

调试器-维基百科,免费百科全书 [ Visual Studio 2010中的精通调试-入门指南 [ ^ ]

调试器在这里向您展示您的代码在做什么,并且您的任务是将其与应该做的事情进行比较.
调试器中没有魔术,它没有发现错误,只是可以帮助您.如果代码未按预期执行操作,则说明您接近错误.


In the recursive call, you just discard the result, it is an error.
Do yourself a favor and learn how to use the debugger.

When you don''t understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don''t find bugs, it just help you to. When the code don''t do what is expected, you are close to a bug.


这篇关于为什么我得到0而不是2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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