+ 1
Alguien que me explique donde estoy cometiendo el error, por favor.
string[] words = { "home", "programming", "victory", "C#", "football", "sport", "book", "learn", "dream", "fun" }; string letter = Console.ReadLine(); int num = 10; for (int count=0;count<num;count++){ if (words[count].Contains(letter)){ Console.WriteLine(words[count]); } else {Console.WriteLine("No match"); break;} }
4 odpowiedzi
+ 1
The problem is "break;" at first non-matching iteration. You should use a boolean set to false and only break when a match occurs, setting the boolean to true. Then test the condition outside the loop
+ 1
Pero necesito que el código tenga de salida dos coincidencias o mas y si uso break, me muestra solo una de las dos coincidencias. no se que debo hacer.
0
Then use a counter instead of a boolean, increase it on matches and use break if it reached your amount
Something like:
int counter = 0;
for (int count = 0; count < num; count++) {
if (words[count].Contains(letter))
counter++;
if (counter == MAX)
break;
}
0
Perfect, Thank you so much.