简体   繁体   English

等于、小于或大于-我错过了什么路径? 为什么说“并非所有代码路径都返回值”?

[英]Equal to, less or greater then - what path am I missing? Why does is say "not all code paths return a value"?

I wonder why this gives me an error message.我想知道为什么这会给我一条错误消息。 What is the path that is missing?缺少的路径是什么? Equal to, less or greater than - that should cover all paths right?等于,小于或大于-应该涵盖所有路径,对吗? PS. PS。 I'm new to programming.我是编程新手。

 public string LargestNumber(int num1, int num2)
    {
        if (num1 > num2)
            return("number 1 is the greatest!");
            
        if (num1 < num2)
            return("number 2 is the greatest!");
            
        if (num1 == num2)
            return("Both are equal!");
                
    }

Well, in many a cases (not int , but say double ) we can have incomparable values where we can't say if they are equal or one of them is larger or smaller.好吧,在许多情况下(不是int ,而是说double ),我们可以有无法比较的值,我们不能说它们是否相等或者其中一个更大或更小。 The compiler aware of it (but it doesn't know the exact int comparison implementation) so it complains: what if num1 and num2 are incomparable ?编译器知道它(但它不知道确切的int比较实现)所以它抱怨:如果num1num2无法比较怎么办? And all num1 > num2 , num1 < num2 , num1 == num2 return false ?并且所有num1 > num2num1 < num2num1 == num2都返回false What should be returned in such a case?在这种情况下应该退回什么?

The easiest solution for you is to drop the last condition:最简单的解决方案是删除最后一个条件:

public string LargestNumber(int num1, int num2)
{
    if (num1 > num2)
        return("number 1 is the greatest!");
            
    if (num1 < num2)
        return("number 2 is the greatest!");
            
    // We know, that there's one option here : to be equal 
    // Compiler doesn't know that all ints are comparable
    return("Both are equal!");
}

please, note that in case of the same code but for double the complainment makes sence.请注意,如果使用相同的代码但要double ,则投诉是有道理的。 Incomparable double values exist:存在无与伦比的double值:

public string LargestNumber(double num1, double num2)
{
    if (num1 > num2)
        return("number 1 is the greatest!");
            
    if (num1 < num2)
        return("number 2 is the greatest!");
            
    if (num1 == num2)
        return("Both are equal!");

    return "Oops!";
}

Demo:演示:

// double.NaN - Not a Number
// if floating point value is not a number, we can't just compare it!
// all >, <, == will return false!
Console.Write(LargestNumber(double.NaN, double.NaN));

Output: Output:

Oops!

I've now updated it and it seem to work..我现在已经更新了它,它似乎工作..

 public string SumOfNumber(int num1, int num2)
    {
        if (num1 > num2)
            return("number 1 is the greatest!");
            
        else if (num1 < num2)
            return("number 2 is the greatest!");
            
        else 
            return("Both are equal!");
            
    }
    

The compiler is not smart enough(better: it does not do all your work) to check that it's impossible that this value is never greater/smaller/equal than another value.编译器不够聪明(更好的是:它不会做所有的工作)来检查这个值永远不会大于/小于/等于另一个值是不可能的。 It says you: do your work and ensure that it's impossible.它说你:做你的工作并确保它是不可能的。 Easy to fix though:不过很容易修复:

Just remove the last if :只需删除最后一个if

public string LargestNumber(int num1, int num2)
{
    if (num1 > num2)
        return "number 1 is the greatest!";
        
    if (num1 < num2)
        return "number 2 is the greatest!";
        
    return "Both are equal!";
}

You should definitely use an else if construct with an else clause at the end.您绝对应该在末尾使用带有 else 子句的 else if 结构。 There is no default path in your code that will be executed "no matter what"您的代码中没有“无论如何”都会执行的默认路径

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

相关问题 为什么这个代码块会说“并非所有代码路径都返回一个值”? - Why does this code block say “not all code paths return a value”? 为什么会出现此错误:并非所有代码路径都返回值? - Why am I getting this error: not all code paths return a value? 为什么编译器会抱怨“当所有代码路径都没有返回值”时,我可以清楚地看到它们的作用? - Why does the compiler complain that 'not all code paths return a value' when I can clearly see that they do? 我的 c# 代码中出现“并非所有代码路径都返回值”错误 - I am getting a 'not all code paths return a value' error in my c# code 为什么这个async / await代码生成“...并非所有代码路径都返回一个值”? - Why does this async / await code generate “…not all code paths return a value”? 循环内的异常处理。 为什么此代码给出的不是所有代码路径都返回值错误? - Exception handling within loop. Why does this code give a not all code paths return a value error? 如果在foreach中使用时为什么会得到“并非所有路径都返回值”的原因 - Why Am I getting “Not All paths return a value” when using if inside a foreach 并非所有代码路径都返回值&#39;&#39; - not all code paths return a value ' ' 并非所有代码路径都返回值 - Not all code paths return a value 并非所有代码路径都返回一个值 - not all code paths return a value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM