简体   繁体   English

即使if语句为true,函数也会返回false

[英]Functions returns false even when the if statement is true

Here is the problem: https://leetcode.com/problems/happy-number/ 这是问题所在: https : //leetcode.com/problems/happy-number/

My solution: 我的解决方案:

static int count = 0; 
public static void Main(string[] args)
{
    Console.WriteLine(happyNumber(19));
    Console.ReadLine(); 
}

public static bool happyNumber(int  a)
{
    double result = 0;
    Stack<int> stapel = new Stack<int>(); 
    //Split the integer into single digits and save them in a stack
    while (a.ToString().Count() > 1)
    {
        stapel.Push(a % 10);
        a = a / 10;
    }
    if (a.ToString().Count() == 1)
    {
        stapel.Push(a);
    }
    // Add the square of the digits to get the result
    foreach (var item in stapel)
    {
        result += Math.Pow((double)item, 2);
    }
    // Check if it's a happy number
    if(result == 1.0)
    {
        return true;
    }
    // counter to stop if it is a endless loop
    else if(count < 100)
    {
        count++;
        happyNumber((int)result);
    }
    return false;
}

So the input 19 is a happy number and the if clauses is true in the 4th run. 因此,输入19是一个幸福数,而if子句在第四轮中为true。 You can set a breakpoint at if(result == 1.0) to check it. 您可以在if(result == 1.0)处设置一个断点进行检查。 So why my function returns false instead? 那么为什么我的函数返回false呢?

Your function is recursive, but you don't do anything with the result of the recursive call. 您的函数是递归的,但是您对递归调用的结果不做任何事情。

If you change: 如果您更改:

happyNumber((int)result);

To: 至:

return happyNumber((int)result);

Then your result for 19 is true . 那么您的19的结果为true There are potentially other issues with the comparison of floating point numbers, but that's probably your main problem! 比较浮点数还可能存在其他问题,但这可能是您的主要问题!

You're unnecessarily casting to a double. 您不必要将其翻倍。 Make result an int rather than a double (or make it a long if you're concerned the result will be too large for an int ). result设为int而不是double (或将其设为long如果您担心结果对于int而言会太大)。 Replace the call to Math.Pow with manually squaring item , like so: Math.Pow的调用替换为手动平方的item ,如下所示:

result += item * item;

The reason that control flow does not enter the if(result == 1.0) block is due to the way floating-point values are internally represented. 控制流未进入if(result == 1.0)块的原因是由于浮点值在内部表示的方式。 Testing for equality between double s is problematic, and so (in this scenario) you should probably just avoid using them entirely as they are unneeded. 测试double s之间的相等性是有问题的,因此(在这种情况下)您可能应该避免完全使用它们,因为它们是不必要的。

You also have a recursive call here: 您还可以在此处进行递归调用:

happyNumber((int)result);

However, that call does nothing, as you're not actually doing anything with the return value. 但是,该调用不会执行任何操作,因为您实际上并没有对返回值做任何事情。 Consider replacing that line with: 考虑用以下方式替换该行:

return happyNumber((int)result);

This will return the value of the recursive call, rather than just discarding it. 这将返回递归调用的值,而不仅仅是丢弃它。

发生这种情况是因为您的happyNumber方法调用了自己(最后一行的第3行),然后从该调用中return truereturn true行-但这仅将堆栈向上返回了happyNumber方法的堆栈....,然后单击了return false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM