请为我找到错误... [英] please find the error for me...
本文介绍了请为我找到错误...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
公共 字符串 getage(如果(年龄> 60 )// 错误:并非所有代码路径都返回值.
返回 " ;
其他
如果(年龄> = 40 & ;& age < = 59 )
返回 " ;
其他
如果(年龄> = 20 & ;& age < = 39 )
返回 " ;
其他
如果(年龄> = 12 & ;& age < = 19 )
返回 " ;
}
它没有收到错误..请帮助一个新蜜蜂..thx
解决方案
如果年龄小于12岁,您的If-case不处理该方案.仔细看.因此,对于age < 12
,您的代码不会返回任何内容,因此也不会返回错误.
我想,您正在尝试了解错误消息的原因.
当您的方法返回String
时,编译器期望每个条件都应返回String
.
在示例中查看以下代码:
如果(年龄> = < = 返回 " ;
它没有"else
"块,因此,在这种情况下,编译器看不到任何String
值被返回.因此,它无法编译该类并显示错误.
希望我能为您提供帮助.
Shikhar,
您已经收到足够的答案;他们告诉您您需要其他",这都是正确的.
但是,我个人认为这只是技术细节.
您的问题是概念性的:您不理会孩子(完全,包括新生儿,婴儿...),但您不应该这样.与大多数软件开发人员不同,孩子们的头脑仍然乌云密布.
因此,这是为您提供的最终答案:如果您不小心忘记了其他",则添加它,如果您忽略孩子,请重新考虑生活.
最好的祝愿! ;)
public String getage(int age)
{
if (age > 60) // Error : Not all code paths return a value.
return "senior";
else
if (age >= 40 && age <= 59)
return "middle aged";
else
if (age >= 20 && age <= 39)
return "adult";
else
if (age >= 12 && age <= 19)
return "teenaged";
}
it do not get the error in it..please help a newbee..thx
解决方案
Your If-case does not handle the scenario if age is less than 12. Look closely. Thus, for age < 12
, your code does not return anything and hence the error.
I guess, you are trying to know the reason of the error message.
As your method returns aString
, the complier expects that each and every condition should return aString
.
Look the following piece of code in your example:
if (age >= 12 && age <= 19) return "teenaged";
It doesn''t have an "else
" block, so, the compiler doesn''t see anyString
value getting returned in this situation. So, it is not able to compile the class and showing the error.
I hope I was able to help you.
Shikhar,
You already received enough answers; they tell you that you need "else" and this is all correct.
However, I personally think this is just a technical detail.
You problem is rather conceptual: you ignore children (totally, with newborn, infants...), but you should not. Unlike most of the software developers, children still have unclouded mind.
So, here is an ultimate answer for you: if you just accidentally forgot about "else" -- add it, if you ignore children -- rethink your life.
Best wishes! ;)
这篇关于请为我找到错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文