In your code, the expression is c <= 'a' && c >= 'z'
They can't be both true, so it is always false. You probably mean to write c >= 'a' && c <= 'z'
Why is this returning false?
题
So I have this check method that returns true if all the parameters are met, and returns false if not. My question is this... Why does (c <= 'a' && c >= 'z' || c == '/') equate to false when an input of:
fiddler 01010100 10011100 /ro ?
*Line is marked
public static bool check(string action)
{
string[] arguments = action.Split(' ');
if (arguments.Length > 3)
{
string[] time = { arguments[1], arguments[2] };
for (int i = 1; i < time.Length; i++)
{
if (i == 1 || i == 2)
foreach (char c in time[i])
{
if (c >= '0' && c <= '9') { }
else return false;
}
else break;
}
for (int i = arguments.Length - 1; i >= 3; i--)
{
if (i != 1 || i != 2 && arguments[i] != "")
{
foreach (char c in arguments[i])
{
>>> >>> >>> if (c <= 'a' && c >= 'z' || c == '/') { }
else return false;
}
if (arguments[i] == " " || arguments[i] == null || call.arguments.Contains(arguments[i]) == true) { }
else return false;
}
}
}
else if (arguments.Length == 3)
{
for (int i = 1; i <= arguments.Length; i++)
{
if (i == 1 || i == 2)
{
foreach (char c in arguments[i])
{
if (c >= '0' && c <= '9') { }
else return false;
}
}
else
{
foreach (char c in arguments[i].ToUpper())
{
if (c <= 'a' && c >= 'z' || c == '/') { }
else return false;
}
if (arguments[i] == " " || arguments[i] == null || call.arguments.Contains(arguments[i]) == true) { }
else return false;
}
}
}
else return false;
return true;
}
解决方案
其他提示
Your code has so many errors.
Firstly, in this part of code:
string[] time = { arguments[1], arguments[2] };
for (int i = 1; i < time.Length; i++)
{
if (i == 1 || i == 2)
i
never be equal to 2
, because (2 >= time.Length
). You should use this:
for (int i = 0; i < time.Length; i++)
without any checking.
Secondly, this part:
for (int i = arguments.Length - 1; i >= 3; i--)
{
if (i != 1 || i != 2 && arguments[i] != "")
i
never be equal to 1
or 2
, beacause this checking already been in for
statement. You should remove this checking for clearing your code.
Thirdly, this check c <= 'a' && c >= 'z'
is always false, because 'z' > 'a'
. You probably should use this c >= 'a' && c <= 'z'
.
Good luck and will be more careful!
不隶属于 StackOverflow