簡體   English   中英

無法遍歷此項目

[英]Unable to iterate through this project

給你的程序定義了一個包含 10 個單詞的數組,並以一個字母作為輸入。 編寫一個程序來遍歷數組和包含所取字母的 output 單詞。 如果沒有這個詞,程序應該 output “不匹配”。

樣本輸入u

樣品 Output 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");
                }
            }
        }
    }
}

既然你用

  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");

嘗試這個

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");}
            }
            
        }
    }
}

使用系統;

命名空間 TestConsoleApp { 內部 class 程序 { 私有 static void Main(string[] args) { string[] words = { “home”, “programming”, “victory”, “C#”, “football”, “sport”, “book ", "學習", "夢想", "樂趣" };

        string letter = Console.ReadLine();

        int count = 0;
        // Use the for loop below

        for (int a = 0; a < words.Length; a++)
        {
            if (words[a].Contains(letter))
            {
                Console.WriteLine(words[a]);
                count++;
            }
        }

        if (count == 0)
        {
            Console.WriteLine("No match");
        }
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM