Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

c# - 如何设置以下意外事件?(How do I program the following contingency?)

I have an array with names of pets and another parallel array with the names of the sounds they make.

(我有一个包含宠物名称的数组,另一个有包含它们发出的声音名称的并行数组。)

I'm writing a method ShowSounds() which asks user for an animal name, and then displays the respective sound.

(我正在编写一种ShowSounds()方法,该方法向用户询问动物名称,然后显示相应的声音。)

How do I code in an error message that says "Sorry that animal isn't in our list" if the user enters something random?

(如果用户随机输入内容,如何在错误消息中写“对不起,该动物不在我们的列表中” ?)

The problem I have right now is that with the if statement, it displays the error message four times even if I enter the correct animal.

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

public static void ShowSound(string userInput2, string[] localPets4, string[] localSounds2)
{
    for (int l = 0; l < localPets4.Length; l++)
    {
        if (userInput2 == localPets4[l])
        {
            Console.WriteLine("{0} makes the sound {1}", localPets4[l], localSounds2[l]);
            Console.WriteLine();
        }
        else
        {
            Console.WriteLine("Sorry that item isn't in our list of animals");
        }
    }
}
  ask by shanko translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

(尝试这个:)

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

The line localPets4.Zip(localSounds2, (pet, sound) => new { pet, sound }) is matching element for element in the localPets4 and localSounds2 arrays and creates a single enumerable in the form of new { pet, sound } .

(行localPets4.Zip(localSounds2, (pet, sound) => new { pet, sound })localPets4localSounds2数组中的element元素匹配,并以new { pet, sound }的形式创建一个可枚举的元素。)

The .Where(x => x.pet == userInput2) only retains the elements where pet equals the userInput2 .

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

This should mean that if there's a match then there's only one match and if there isn't a match then the enumerable is empty.

(这意味着如果有匹配项,那么只有一个匹配项;如果没有匹配项,则可枚举为空。)

Calling .FirstOrDefault() then either you have the single new { pet, sound } that manages the input or you have null .

(调用.FirstOrDefault()然后您将拥有一个管理输入的new { pet, sound }或拥有null 。)

The rest of the code simply produces the output based on the result .

(其余代码只是根据result生成输出。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...