返回操作在IF条件下不起作用 [英] Return operate doesn't work in IF condition
本文介绍了返回操作在IF条件下不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static bool IsInteger(object input)
{
bool Valid = false;
if(输入为字符串)
{
int Number;
while(有效== false)
{
if(int.TryParse(输入为字符串,输出数字))有效=真;
休息;
}
return有效;
}
// int a;
// int b;
// if(0 == 0)
// {
// a = 1;
// while(a< 10)
// {
// if(a< 5)b = a;
//休息;
//}
//返回true;
//}
我已经声明了一种将输入验证为整数的方法。我已经返回变量Valid作为布尔值,但
编译器仍然说"所有代码路径必须返回一个值"。在注释行中,我尝试了一些简单的代码,
语法相同,并且工作清晰。我希望返回值在条件中,而不是在外面。如果有人能够表明我的错误,我将不胜感激。谢谢。
解决方案
public static bool IsInteger(对象输入)
{
bool有效期= false;
if(输入为字符串)
{
int Number;
while(有效== false)
{
if(int.TryParse(输入为字符串,输出数字))有效=真;
休息;
}
return有效;
}
return有效;
}
现在,所有路径都返回一个值
public static bool IsInteger(object Input) { bool Valid = false; if (Input is string) { int Number; while (Valid == false) { if (int.TryParse(Input as string, out Number)) Valid = true; break; } return Valid; } //int a; //int b; //if (0 == 0) //{ // a = 1; // while (a < 10) // { // if (a < 5) b = a; // break; // } // return true; //}
I have declared a method to validate an input as integer. I have return the variable Valid as boolean but the
compiler still say "all codes path must return a value". In the comment lines I have tried some simple codes with
the same syntax and it work clearly. I want the return value is in the condition, not outside. If someone can show
me what I was wrong, that would be appreciated. Thank you.
解决方案public static bool IsInteger(object Input) { bool Valid = false; if (Input is string) { int Number; while (Valid == false) { if (int.TryParse(Input as string, out Number)) Valid = true; break; } return Valid; } return Valid; }Now, all paths return a value
这篇关于返回操作在IF条件下不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文