![](/img/trans.png)
[英]Outlook add-in - unable to iterate through all mailitems in Outlook.Folder using foreach loop
[英]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.