我在公司接受采访时被问到一个问题,问题有点奇怪,所以想和专家一起询问。
问题假设我有返回bool类型的函数。我们这样说:
public bool func(int param)
{
bool retVal;
// here is some algorithm which as a result either set the retVal to false or true,
// It doesn't matter what is algo, the only thing important is it either do
// retVal=false or retVal=true
// The question is i have to write the algo here which in case
// if the previous algo gives us retVal=false then it should
// return true and if retVal=true then return false
}
算法应该是什么?
答案 0 :(得分:0)
我认为你的代码是这样的:
public bool func(int param)
{
bool retVal;
return !retVal;
}
答案 1 :(得分:0)
!retVal与retVal相反。如果retVal为true,则!retval为false,反之亦然
public bool func(int param)
{
bool retVal
//your algo;
return !retVal;
}
答案 2 :(得分:-1)
如果算法的结果是retval,
return (!retval);