[英]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
比较实现)所以它抱怨:如果num1
和num2
无法比较怎么办? And all num1 > num2
, num1 < num2
, num1 == num2
return false
?并且所有num1 > num2
, num1 < num2
, num1 == 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.