+ 1
Can anyone tell me why this code loops true ?
This code loops true and I want to know why and how to stop it. https://code.sololearn.com/cf6WrHQR8L01/?ref=app
6 ответов
+ 3
What are you trying to achieve?
+ 2
put break statement
if (char.IsNumber(e) || e == 1 || e == 0)
{
Console.WriteLine(true);
break;
}
+ 1
string input = "101101";
char[] convert = input.ToCharArray();
foreach (var e in convert)
{
if (char.IsNumber(e) || e == 1 || e == 0)
{
Console.WriteLine(true);
}
else
{
Console.WriteLine(false);
}
}
isNumber(Char) indicates whether the specified character passed is number or not?
so:
e is always either 1 or 0 based on your input( "101101"))
if (char.IsNumber(e) || e == 1 || e == 0) since e = 1 and we know 1 is numeric(number) so it's return true.
Try to replace
if (char.IsNumber(e) || e == 1 || e == 0)
with
if (e == 1 || e == 0)
and see the difference.
+ 1
Rkk thanks for your answer, but it returns false, instead of true. I want to remove the loop. I want it to say true once.
+ 1
I'm trying to stop the foreach loop from looping the output. Because the output always loops.