0

Слова, помогите пожалуйста

Вам дана программа, которая задает массив из 10 слов и принимает букву в качестве вводных данных. Напишите программу для перебора массива и вывода слов, содержащих принятую букву. При отсутствии подходящих слов, программа должна выводить "No match". Пример Входных Данных u Пример Выходных Данных fun

19th May 2021, 1:13 AM
Александр Шефер
Александр Шефер - avatar
8 Antworten
+ 3
Александр Шефер If {letter} is meant to store a single character from user input, try using: var letter = Console.ReadChar(); Then, you'll want to loop through the {words} string array and check each {word} to see if it contains the {letter}. foreach(var word in words) { if(word.Contains(letter)) { Console.WriteLine(word); } } Remember, the variables contain values. If the value is a collection of values, like your array, you'll likely need to loop through the collection to check each value. Notice how the foreach loop stores the current value in the loop to a variable called {word}. You can work with this variable in the context of the current loop cycle. Another basic loop you can use is the for loop: for(var i=0; i < words.Length; i++) { var word = words[i]; if(word.Contains(letter)) { Console.WriteLine(word); } } See if you can spot the similarities and differences. Hopefully, this helps connect some dots on the approach. 👌
19th May 2021, 1:43 AM
David Carroll
David Carroll - avatar
+ 3
Александр Шефер There are several ways to determine "No match." However, I'll keep it simple by building from the loop example. I'll also describe the code so you can attempt to get it working. 😉 The approach is to declare a new int variable named {wordsMatched} initialized to 0. This will be placed before the loop. Next, within the if block of the loop, add a line before or after Console.WriteLine(word) where the value for {wordsMatched} is incremented by 1. NOTE: This must be between the curly brackets of the if statement. Therefore, when a match is found in the loop, the word is printed to console and the {wordsMatched} value is increased by 1. After the loop, check if the value {wordsMatched} is equal to 0. If it is, then print "No match." Hopefully, this explanation gives you some guidance on how to do this second part. 👌
19th May 2021, 3:55 AM
David Carroll
David Carroll - avatar
+ 2
Александр Шефер I'm not sure which challenge your attempting. Do you have a link?
19th May 2021, 2:20 AM
David Carroll
David Carroll - avatar
+ 1
Я вообще не могу предположить как это делается, у меня есть код который делает перебор, но он немного не тот using System; using System.Collections.Generic; 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(); if(letter.Contains("l")) Console.WriteLine("football"); Console.WriteLine("learn"); } } }
19th May 2021, 1:15 AM
Александр Шефер
Александр Шефер - avatar
0
Thank you very much
19th May 2021, 1:47 AM
Александр Шефер
Александр Шефер - avatar
0
David Carroll I see you made a good ststema, but I do not even suggest how to make the second answer No match, I tried to make it using the else method, but it did not work
19th May 2021, 2:05 AM
Александр Шефер
Александр Шефер - avatar
0
David Carroll If there are no matching words, the program should output "No match".
19th May 2021, 2:27 AM
Александр Шефер
Александр Шефер - avatar
0
using System; using System.Collections.Generic; 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 i = 0; i<words.Length; i++) { if(words[i].Contains(letter)) { Console.WriteLine(words[i]); } } } } }
23rd Aug 2024, 5:13 PM
TEMNЫY HAKER
TEMNЫY HAKER - avatar