How do I get "No match" just once to the screen if entered key or word is not in the array list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I get "No match" just once to the screen if entered key or word is not in the array list?

I made a code if the entered key is in the array so the words will be listed in different lines,but if the key is not ,then there should be just one "No match" to the screen My code///// using System; using System.Collections.Generic; using System.Linq; namespace Code_Coach_Challenge { class Program { static void Main(string[] args) { string[] words = { "home", "programming", "victory", "C#", "football", "sport", "book", "learn", "dream", "fun" }; string letter = Console.ReadLine(); for( int count = 0;count<10;count++){ if(words[count].Contains(letter)) { Console.WriteLine(words[count]); }} } } }

18th Oct 2021, 1:42 PM
Rubayet Kamal
Rubayet Kamal - avatar
2 Answers
+ 2
Make a flag(bool). Let's say it's called noMatch and initialized with true. When there is any match, i.e. the code manage to execute the if statement, you make it false. After the loop ends, you can use this flag to determine whether to print "No match".
18th Oct 2021, 2:10 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
This could be done like this: string letter = Console.ReadLine(); Boolean found = false; for( int count = 0;count<10;count++) { if(words[count].Contains(letter)) { Console.WriteLine(words[count]); found = true; } } if (!found) { Console.WriteLine("Not found"); } Set a flag (found) to false. Set this to true if something is found. After the the loop check if it is false or true and print a message if it is false.
18th Oct 2021, 2:20 PM
Coding Cat
Coding Cat - avatar