Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何规划以下意外事件?_C#_Arrays_For Loop_If Statement - Fatal编程技术网

C# 我如何规划以下意外事件?

C# 我如何规划以下意外事件?,c#,arrays,for-loop,if-statement,C#,Arrays,For Loop,If Statement,我有一个包含宠物名字的数组,另一个平行数组包含它们发出的声音的名字。我正在编写一个方法ShowSounds(),它要求用户输入动物名称,然后显示相应的声音 如果用户输入了一些随机的内容,我该如何编写一条错误消息,上面写着“对不起,动物不在我们的列表中” 我现在遇到的问题是,使用if语句,即使我输入了正确的动物,它也会显示错误消息四次 publicstaticvoidshowsound(字符串userInput2,字符串[]localPets4,字符串[]localSounds2) { for(i

我有一个包含宠物名字的数组,另一个平行数组包含它们发出的声音的名字。我正在编写一个方法
ShowSounds()
,它要求用户输入动物名称,然后显示相应的声音

如果用户输入了一些随机的内容,我该如何编写一条错误消息,上面写着“对不起,动物不在我们的列表中”

我现在遇到的问题是,使用
if
语句,即使我输入了正确的动物,它也会显示错误消息四次

publicstaticvoidshowsound(字符串userInput2,字符串[]localPets4,字符串[]localSounds2)
{
for(int l=0;l
试试这个:

public static void ShowSound(string userInput2, string[] localPets4, string[] localSounds2)
{
    var result =
        localPets4
            .Zip(localSounds2, (pet, sound) => new { pet, sound })
            .Where(x => x.pet == userInput2)
            .FirstOrDefault();

    if (result != null)
    {
        Console.WriteLine($"{result.pet} makes the sound {result.sound}");
        Console.WriteLine();
    }
    else
    {
        Console.WriteLine($"Sorry, {userInput2} isn't in our list of animals");
    }
}
localPets4.Zip(localSounds2,(pet,sound)=>new{pet,sound})
localPets4
localSounds2
数组中匹配元素,并以
new{pet,sound}
的形式创建单个可枚举项

.Where(x=>x.pet==userInput2)
只保留
pet
等于
userInput2
的元素

这意味着如果存在匹配项,则只有一个匹配项,如果没有匹配项,则可枚举项为空。调用
.FirstOrDefault()
则可以使用单个
新的{pet,sound}
来管理输入,或者使用
null

代码的其余部分只是根据您可以使用的
结果生成输出
Array.Exists(localPets4,x=>x.Equals(userInput2))
检查输入是否有效,然后通过循环查找声音

if (Array.Exists(localPets4, x => x.Equals(userInput2))) {
    for (int l = 0; l < localPets4.Length; l++)
    {
        if (localPets4[l].Equals(userInput2) {
            Console.WriteLine("{0} makes the sound {1}", localPets4[l], localSounds2[l]);
            Console.WriteLine();
            break;
        }
    }
}
else
    {
         Console.WriteLine("Sorry that item isn't in our list of animals");
    }
if(Array.Exists(localPets4,x=>x.Equals(userInput2))){
for(int l=0;l

但这只会检查unserInput2是否完全匹配。如果需要近似匹配,则必须更详细一些。

因为您只想打印该语句一次(如果未找到任何项),因此需要将其从循环中删除:

public static void ShowSound(string userInput2, string[] localPets4, string[] localSounds2) 
{ 
    bool itemFound = false;

    for (int l = 0; l < localPets4.Length; l++) 
    { 
        if (userInput2 == localPets4[l])
        { 
            itemFound = true;
            Console.WriteLine("{0} makes the sound {1}", localPets4[l], localSounds2[l]);
            Console.WriteLine(); 
        }
    } 

    if (!itemFound)
    {
        Console.WriteLine("Sorry that item isn't in our list of animals"); 
    }
}
publicstaticvoidshowsound(字符串userInput2,字符串[]localPets4,字符串[]localSounds2)
{ 
bool itemFound=false;
for(int l=0;l与数组遍历相关的
。如果
string[]localPets4
包含4个元素,则
for循环将迭代4次-ergo 4错误消息。
因此,在for循环中,您应该只执行搜索。然后,在成功/失败时向用户反馈的逻辑将在
for循环之外处理

int foundAtIndex = -1;

for (int l = 0; l < localPets4.Length; l++) 
{ 
    if (userInput2 == localPets4[l])
    { 
        foundAtIndex = l;
    }
}
if (foundAtIndex != -1)
{
        Console.WriteLine("{0} makes the sound {1}", localPets4[foundAtIndex ],localSounds2[foundAtIndex ]);
        Console.WriteLine(); 
}
else 
{
    Console.WriteLine($"Sorry, {userInput2} isn't in our list of animals");
    Console.WriteLine(); 
}
int foundAtIndex=-1;
for(int l=0;l

由于这是一对一的关系,您可以使用
[字典][1]
,但我不确定这是否超出了您的应用程序范围。

else
分支需要在您的
for
循环之外。只有在您检查了所有元素后,您才会知道该项不在您的列表中。我该如何设置格式?如果我将
else
添加到for循环之外,那么它就不存在了r使用
if
语句找到匹配项后,您还应该使用
break
离开循环。@shanko只是分支,没有
else
关键字是的,对不起-我刚刚捕捉到了。它已被编辑。如果您不需要循环做任何其他事情,那么我同意@Engimativity。但是,如果您需要更多近似值,请再说一次匹配你必须要有创意。这将是一种更直接的方式。它在一次调用中进行搜索并返回准确的索引,不需要额外的循环。@Peter Duniho,我同意并感谢你的评论。但正如你对Enigmativity所说的,我觉得这是一个适合某种教育冒险的问题。我不确定由于某种原因需要e循环,IndexOf()方法将有助于消除这种情况。当然,但您的答案并不真正符合“教育背景”,因此我认为您最好对此保持高效。:(如果您想知道我所说的“教育背景”是什么意思的话,您的答案显著地改变了代码的整体结构,甚至不是以一种理想的方式--它强制对数组进行两次扫描,而不是仅进行一次扫描…还有两个其他答案与原始答案更接近,保留其原始结构,同时仍然只扫描数组一次。)嗯…嗯,我不能因为代码的正确性而责备你。:)但是,考虑到上下文,这有点奇怪。OP几乎肯定是在某种教育背景下写这篇文章的,无论是课堂还是他们自己的独立学习。我想你