+ 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;} }

19th May 2022, 2:38 AM
Andrey
4 ответов
+ 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
19th May 2022, 9:32 AM
OrHy3
OrHy3 - avatar
+ 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.
19th May 2022, 7:44 PM
Andrey
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; }
20th May 2022, 12:22 AM
OrHy3
OrHy3 - avatar
0
Perfect, Thank you so much.
20th May 2022, 1:43 AM
Andrey