【问题标题】:Unable to iterate through this project无法遍历此项目
【发布时间】:2021-10-12 23:05:08
【问题描述】:

给你的程序定义了一个包含 10 个单词的数组,并以一个字母作为输入。 编写一个程序来遍历数组并输出包含所取字母的单词。 如果没有这个词,程序应该输出“No match”。

示例输入 u

样本输出 fun

/////////////////////////////////////////The Code I did comes after this//////////////////
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 count = 0; count < 10; count++)
            {
                if (words.Contains(letter)) { Console.WriteLine(words[count]); }
                else
                {
                    Console.WriteLine("No match");
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# c#-4.0 c#-3.0


    【解决方案1】:

    自从你使用

      if (words.Contains(letter))
    

    应用程序一个接一个地检查整个数组,并将每个完整的单词与字母进行比较。如果字母是整个世界,并且它在数组单元之一中找到这个世界,则返回 true 和单词 words[count]。但是 words[count] 可能是任何单词,而不仅仅是包含正确字母的单词。要返回正确的单词,您应该使用

     if ( words[count].Contains(letter))
    

    在这种情况下,应用程序只查找每个单词中的子字符串,而不是整个单词等于一个字母。

    所以要在数组中找到正确的单词,您需要使用数组索引。试试这个

    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();
                var found = false;
                var i = 0;
                var list = new List<string>();
                for ( i = 0; i < words.Length; i++)
                {
    
                    if (words[i].Contains(letter))
                    {
                        found=true;
                        list.Add(words[i]);
                    
                    }
        
                }   
                 if (found)  Console.WriteLine("Found word(s): "+ string.Join(",", list));
                 else Console.WriteLine("No match");
            }
        }
    }
    

    输入“o”的输出

    Found word(s):home,programming,victory,football,sport,book
    

    更新

    为学生提供更简单的变体

     string[] words = { "home", "programming", "victory", "C#", "football", "sport", "book", "learn", "dream", "fun" };
    
                string letter = Console.ReadLine();
                var found = false;
    
                for ( var i = 0; i < words.Length; i++)
                {
                    if (words[i].Contains(letter))
                    {
                        found=true;
                        Console.WriteLine(words[i]);
                     }
                  }   
                 if (!found) Console.WriteLine("No match");
    

    【讨论】:

    • 谢谢,但要求的输出不同,例如 //////////////////////// ///////////////////////////////////////// ///////////////////////////找到的单词:主页,编程,胜利,足球,运动,书籍未预期;所需的输出就是这样的家庭\n编程
    • @RKEshat 我在答案中添加了更新
    【解决方案2】:

    试试这个

    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();
                bool found = false;
                
                
                for (int count = 0; count < words.Length ; count ++ )
                {
                    if (words[count].Contains(letter))
    
                    {
                        found = true;
                        Console.Write(words[count]);
                    }
     
                }
    
                if (!found)
                {
                  {Console.Write("No match");}
                }
                
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 2021-11-21
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多